How to create schema for Oracle APEX Workspace?


If you choose to create a schema while creating the workspace in Oracle Application Express administration service, APEX will create a brand new tablespace as well. This may not be an ideal situation. Having too many tablespaces, specially when it is not required, adds management and administrative overhead.

You can use the following script to create the schema and then just use this schema when you are creating the worksace.

— Connect as SYSTEM or DBA User
$ sqlplus system/password@XE

— Create the new Schema/User
CREATE USER APEXAPPS IDENTIFIED BY APEXAPPS
DEFAULT TABLESPACE APEXAPPS_TS
TEMPORARY TABLESPACE TEMP;

— Grant quota on assigned tablespace
ALTER USER APEXAPPS QUOTA 10M ON APEXAPPS_TS ;

— Grant Role Priviledge
GRANT “CONNECT” TO APEXAPPS ;
ALTER USER APEXAPPS DEFAULT ROLE “CONNECT”;

— Grant system privileges
GRANT CREATE JOB TO APEXAPPS ;
GRANT CREATE INDEXTYPE TO APEXAPPS ;
GRANT CREATE SYNONYM TO APEXAPPS ;
GRANT CREATE DIMENSION TO APEXAPPS ;
GRANT CREATE VIEW TO APEXAPPS ;
GRANT CREATE CLUSTER TO APEXAPPS ;
GRANT CREATE MATERIALIZED VIEW TO APEXAPPS ;
GRANT CREATE PROCEDURE TO APEXAPPS ;
GRANT CREATE ANY CONTEXT TO APEXAPPS ;
GRANT CREATE TYPE TO APEXAPPS ;
GRANT CREATE SEQUENCE TO APEXAPPS ;
GRANT CREATE OPERATOR TO APEXAPPS ;
GRANT CREATE TRIGGER TO APEXAPPS ;
GRANT CREATE TABLE TO APEXAPPS ;

— Grant Object Privileges
GRANT EXECUTE ON SYS.DBMS_RLS TO APEXAPPS ;
GRANT EXECUTE ON CTXSYS.CTX_DDL TO APEXAPPS ;
GRANT EXECUTE ON CTXSYS.CTX_DOC TO APEXAPPS ;

These commands will create the schema and assign the same privileges to that schema that gets assigned when you create a new schema while creating the APEX workspace (verified in Oracle APEX 4.1). Once the schema is created specify ‘Yes’ for Re-use existing schema and then specify the schema name.

Advertisements

Get the width and height of an image using a function in PHP


This is a very useful function in PHP. PHP provides function to get the width and height of an image. The
getimagesize() function will determine the size of image file including flash file(swf).

Syntax

list($width, $height, $type, $attr) = getimagesize(“image_name.jpg”);

Code

This is a sample code, change image_name to your image name and test it!

<?php

list($width, $height, $type, $attr) = getimagesize(“image_name.jpg”);

echo “Image width ” .$width;
echo “<BR>”;
echo “Image height ” .$height;
echo “<BR>”;
echo “Image type ” .$type;
echo “<BR>”;
echo “Attribute ” .$attr;

?>

When you run this script you will see the result like this

Image width 379
Image height 344
Image type 2
Image attribute width=”379″ height=”344″

You will get the width, height, type of an image and also attribute of an image, I use this function in my image

upload form.

Type of an image you can see from table below You can read a full manual here

Type of the image
1 = GIF      5 = PSD          9 = JPC     13 = SWC
2 = JPG     6 = BMP     10 = JP2     14 = IFF
3 = PNG   7 = TIFF(intel byte order)               11 = JPX     15 = WBMP
4 = SWF   8 = TIFF(motorola byte order)     12 = JB2     16 = XBM

Reset Webmin Password for ubuntu


I found this problem at the first time when webmin installed. Also this happens to everyone at one time or another. You go on vacation and when you come back, you forget all of the passwords on your computer.

I failed logging in several times and then Webmin blocked my IP. Getting localhost blocked isn’t a good thing to see. So I searched the Internet for a way to reset the password and I found this procedure:

1. Login to your computer as root. Type on browser address bar https://localhost:10000/
2. If you are running a Debian distribution (ubuntu), enter the following command:
/usr/share/webmin/changepass.pl /etc/webmin username password
3. Login to Webmin with your reset password.
4. eNjoY!

Select random value from MySQL database table.


The simplest way of selecting random rows from the MySQL database is to use “ORDER BY RAND()” clause in the query.

Solution 1 [SQL QUERY]

SELECT * FROM `table` ORDER BY RAND() LIMIT 0,4;

This query select 4 random value every refresh?

The problem with this method is that it is very slow. The reason for it being so slow is that MySQL creates a temporary table with all the result rows and assigns each one of them a random sorting index. The results are then sorted and returned.

There are several workarounds to speed things up.

The basic idea is to get a random number and then select a specific row using this number.

In the case that all the rows have unique ids we will just have to pick a random number between the smallest and the biggest id and then select the row with id that equals that number. To make this method work when ids are not evenly distributed we will have to use “>=” operator instead of “=” in the last query.

To get the minimum and maximum id values in the entire table we will use MAX() and MIN() aggregate functions. These functions will return minimum and maximum value in the specified group. The group in our case is all the values of `id` column in our table.

Solution 2 [PHP]

$range_result = mysql_query( ” SELECT MAX(`id`) AS max_id , MIN(`id`) AS min_id FROM `table` “);

$range_row = mysql_fetch_object( $range_result );

$random = mt_rand( $range_row->min_id , $range_row->max_id );

$result = mysql_query( ” SELECT * FROM `table` WHERE `id` >= $random LIMIT 0,1 “);

As we mentioned this method is limited to tables with unique id for each row. What to do if it’s not the case?

The solution is to use the MySQL LIMIT clause. LIMIT accepts two arguments. The first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1).

To calculate the offset to the first row we will generate a random number between 0 and 1 using MySQL’s RAND() function. Then we will multiply this number by number of records in the table, which we will get using COUNT() function. Since LIMIT arguments must be integers and not float values we will round the resulting number using FLOOR() function. FLOOR() is an arithmetic function that calculates the largest integer value that is smaller than or equal to the expression. The resulting code will look like this:

Solution 3 [PHP]

$offset_result = mysql_query( ” SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `table` “);

$offset_row = mysql_fetch_object( $offset_result );

$offset = $offset_row->offset;

$result = mysql_query( ” SELECT * FROM `table` LIMIT $offset, 1 ” );

In MySQL 4.1 and later we can combine two previous methods using subquery like so:

Solution 4 [SQL]

SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;

This solution has the same weakness as the solution 2 e.g. it only works for tables with unique ids.

Remember the reason we started looked for alternative ways of selecting random rows? Speed! So how do these methods compare in terms of execution times. I am not going to go into specifics of hardware and software configuration or give precise numbers. The approximate results are:

  • The slowest method is solution 1. Let’s say that it took 100% of time to execute.
  • Solution 2 took 79%.
  • Solution 3 – 13%.
  • Solution 4 – 16%.

The winner is solution 3.

XMLToArray Generator Class


Here is a PHP class that read xml file and make an array. First save this class file as xmltoarray.php and then create an object like new XmlToArray($xml_data). Finally just call createArray() function. It will be helpful when we don’t want to use database or any kind of web services. Bellow i have given full source code. I think it will be helpful.

<?
/**
* XMLToArray : Converting XML into Array
*
* @author  :  MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
*             Moderator, phpResource (http://groups.yahoo.com/group/phpresource/)
*             URL: http://www.rupom.info
* @version :  1.0
* @date       11/03/2006
* Purpose  :  Creating Hierarchical Array from XML Data
*/

class XmlToArray
{

private $xml=”; //To use in PHP4, use “var”

/**
* Default Constructor
* @param $xml = xml data
* @return none
*/

function XmlToArray($xml)
{
$this->xml = $xml;
}

/**
* _struct_to_array($values, &$i)
*
* This adds the contents of the return xml into the array for easier processing.
* Recursive, Static
*
* @access    private
* @param    array  $values this is the xml data in an array
* @param    int    $i  this is the current location in the array
* @return    Array
*/

function _struct_to_array($values, &$i)
{
$child = array();

if (isset($values[$i][‘value’]))
{
array_push($child, $values[$i][‘value’]);
}

while ($i++ < count($values))
{
switch ($values[$i][‘type’])
{
case ‘cdata’:
array_push($child, $values[$i][‘value’]);
break;

case ‘complete’:
$name = $values[$i][‘tag’];
if(!empty($name))
{
$child[$name]= ($values[$i][‘value’])?($values[$i][‘value’]):”;

if(isset($values[$i][‘attributes’]))
{
$child[$name] = $values[$i][‘attributes’];
}
}
break;

case ‘open’:
$name = $values[$i][‘tag’];
$size = isset($child[$name]) ? sizeof($child[$name]) : 0;
$child[$name][$size] = $this->_struct_to_array($values, $i);
break;

case ‘close’:
return $child;
break;
}
}
return $child;

}//EO _struct_to_array

/**
* createArray($data)
*
* This adds the contents of the return xml into the array for easier processing.
*
* @access    public
* @param    string    $data this is the string of the xml data
* @return    Array
*/
function createArray()
{
$xml    = $this->xml;
$values = array();
$index  = array();
$array  = array();
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $xml, $values, $index);
xml_parser_free($parser);
$i = 0;
$name = $values[$i][‘tag’];
$array[$name] = isset($values[$i][‘attributes’]) ? $values[$i][‘attributes’] : ”;
$array[$name] = $this->_struct_to_array($values, $i);
return $array;
}//createArray

}//EO Class XmlToArray

?>

<?php

/**
* phpResourceRssFeed : phpResource Feeds for Your Site
*
* @author  :  MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
*             Moderator, phpResource (http://groups.yahoo.com/group/phpresource/)
*             URL: http://www.rupom.info
* @version :  1.0
* @date       11/03/2006
*/

?>
<title>phpResource Feeds for Your Site</title>
<?php

require_once(“XmlToArray.class.php”);

$xml_data = file_get_contents(“http://rss.groups.yahoo.com/group/phpresource/rss&#8221;);

//Creating Instance of the Class
$xmlObj    = new XmlToArray($xml_data);

//Creating Array
$arrayData = $xmlObj->createArray();

$rssData = array();

$rssData = $arrayData[‘rss’][‘channel’][0][‘item’];

$colors = array(‘#CCCCCC’,’#f5f5f5′);

$numColors = count($colors);

if(!empty($rssData))
{
?>
<table align=”left”>
<tr>
<td>&nbsp;</td>
</tr>
<tr bgcolor=”#6699CC”>
<td colspan=”2″><font size=”5″><b>Latest Posts of <a href=”http://groups.yahoo.com/group/phpresource/”>phpResource Group</a></b></font></td>
</tr>
<tr>
<td colspan=”2″>&nbsp;</td>
</tr>
<?php
$i = 0;
foreach($rssData as $i=>$v)
{

$title   = $v[‘title’];
$link    = $v[‘link’];
$author  = $v[‘author’];
$pubDate = $v[‘pubDate’];
$desc    = $v[‘description’];

?>

<tr bgcolor=”<?=$colors[$i%$numColors];?>”>
<td><a href=”<?=$link?>”><font size=”4″><?=$title?></font></a><br />&nbsp;&nbsp;&nbsp;On <?=$pubDate?> By <b><?=$author?></b></td>
</tr>
<tr>
<td>
<?php

$desc = wordwrap( $desc, 72, “<br>”);
echo “$desc …. “;

//echo $desc.’ <b>…</b> ‘;
?>
<br />
<a href=”<?=$link?>”>Read More…</a>
</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<?php
$i++;
}
}

function dBug($arr)
{
echo “<PRE>”;
print_r($arr);
echo “</PRE>”;
}
?>

<tr bgcolor=”#6699CC”>
<td><a href=”http://groups.yahoo.com/group/phpresource/join/”><font size=”5″>Click Here to Join phpResource</font></a></td>
</tr>

</table>