converted from asp to php and problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ridwan
Forum Commoner
Posts: 55
Joined: Thu Aug 22, 2002 3:15 am

converted from asp to php and problem

Post by ridwan »

Hi all I converted the following code from asp to php the function of the script is to create a drop down box that call sit's values from a db and then when the user selects a value from the drop down it shows specific values in certain fields by the way I am using odbc to connect to my db b'cos I am using a ms sql 2k server.

The thing is it works fine in asp but when I converted it I get an error telling me that the id variable is undefined.

Code: Select all

<body>
<select name="Ref_ID" onchange="doSel(this)">
<option value="">Please select</option>
<? while (odbc_fetch_row($result))
	&#123;?>
<option value='location.href='dropdown.php?id=<? odbc_result($result, 'ContentId'); ?>'"
	<?if (intval($id) == odbc_result($result, 'ContentId'))
	&#123;
	echo " selected";
    &#125;
	?>>
<? odbc_result($result, 'publication') ?> </option>
	<? &#125; ?>

</select>

<?if (empty($id))
&#123;
//Select the resulset that is going to be used to query data
$strSQLa = "SELECT * FROM publications WHERE ContentId = '" . $id  . "'";
$result= odbc_exec( $dbConnection, $strSQLa);

?>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
   <td>publication</td>
   <td><INPUT NAME="publication" TYPE="TEXT" VALUE="<? echo odbc_result($result, 'publication'); ?>">
</td>
 </tr>
 <tr>
   <td>contact</td>
   <td><INPUT NAME="contact" TYPE="TEXT" VALUE="<? echo odbc_result($result, 'contactee'); ?>"></td>
 </tr>
</table>
<? &#125; ?>
thanks all
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please read Sticky: Before Post Read: Concerning Passing Variables in PHP 4.2+
It's not a sticky thread for nothing ;)
ridwan
Forum Commoner
Posts: 55
Joined: Thu Aug 22, 2002 3:15 am

its on by thw

Post by ridwan »

I have turned my register globals on by the way to test this script
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

probably it's only a notice/warning because $id is used wether it has been transmitted or not
e.g.
if (intval($id) == odbc_result($result, 'ContentId'))

Code: Select all

if (isset($id) && intval($id) == odbc_result($result, 'ContentId'))
ridwan
Forum Commoner
Posts: 55
Joined: Thu Aug 22, 2002 3:15 am

i see

Post by ridwan »

I see what that does ,(define the variable and then tel it what ype it is) but I still get an error on the line where I have my second sql query which goes as follows:

Code: Select all

$strSQLa = "SELECT * FROM publications WHERE ContentId = '" . isset($id)  . "'";
Another thing is that it shows nothing in my drop down list but there are values it's as if they're invisible :?
ridwan
Forum Commoner
Posts: 55
Joined: Thu Aug 22, 2002 3:15 am

i see

Post by ridwan »

I see what that does ,(define the variable and then tel it what ype it is) but I still get an error on the line where I have my second sql query which goes as follows:

Code: Select all

$strSQLa = "SELECT * FROM publications WHERE ContentId = '" . isset($id)  . "'";
Another thing is that it shows nothing in my drop down list but there are values it's as if they're invisible :?

I think if I can sort the second problem of there not being anything in the drop down list I can get the second issue to go away 'cos then there will be a value which will be passed to the second sql statement by the way I'm just re-including the code as I changed a couple of things:

Code: Select all

<select name="Ref_ID" onchange="doSel(this)">
<option value="">Please select</option>
<? while (odbc_fetch_row($result))
	&#123;?>
<option value='location.href='dropdown.php?id=<? odbc_result($result, 'ContentId'); ?>'"
	<?if (isset($id) && intval($id) == odbc_result($result, 'ContentId'))
	&#123;
	echo " selected";
    &#125;
	?>>
<? odbc_result($result, 'publication') ?> </option>
	<? &#125; ?>

</select>

<?if (empty($id))
&#123;
//Select the resulset that is going to be used to query data
$strSQLa = "SELECT * FROM publications WHERE ContentId = '" . isset($id)  . "'";
$resulta= odbc_exec( $dbConnection, $strSQLa);

?>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
   <td>publication</td>
   <td><INPUT NAME="publication" TYPE="TEXT" VALUE="<? echo odbc_result($resulta, 'publication'); ?>">
</td>
 </tr>
 <tr>
   <td>contact</td>
   <td><INPUT NAME="contact" TYPE="TEXT" VALUE="<? echo odbc_result($resulta, 'contactee'); ?>"></td>
 </tr>
</table>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

bool isset ( mixed var [, mixed var [, ...]]) returns TRUE if the variable passed as argument exists, FALSE otherwise.
if (empty($id))
the body of the if-statement is only executed if $id is not defined, is == '' or is == '0'.

Code: Select all

/*
 if there is a variable $id 
 and the numeric value of $id is unequal to 0
 casting $id to (int) removes the need for mysql_escape_string or similar
*/
if (isset($id) && ($id = (int)($id)) != 0) 
{
// assuming ContentId is numeric
	//Select the resulset that is going to be used to query data
	$strSQLa = "SELECT * FROM publications WHERE ContentId='" . $id  . "'";
what is
<option value='location.href='dropdown.php?id=...
supposed to do?
ridwan
Forum Commoner
Posts: 55
Joined: Thu Aug 22, 2002 3:15 am

the meaning

Post by ridwan »

you see the first page that you go into is called dropdown.php and when you select a value from the dropdown box it jumps to dropdown.php?id='whatever value the user selected' this is so that the user doesn't have to press on submit each time he changes a value

Here is the javascript for that select box:

Code: Select all

<script language="Javascript">
function doSel(obj)
&#123;
  for (i = 0; i < obj.length; i++)
     if (obj&#1111;i].selected == true)
       eval(obj&#1111;i].value);
&#125;
</script>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

then change
<option value='
to

Code: Select all

<option value="
otherwise the property is always parsed as value='location.href='

btw:

Code: Select all

function doSel(obj)
&#123;
    eval(obj.value);
&#125;
will do the same
ridwan
Forum Commoner
Posts: 55
Joined: Thu Aug 22, 2002 3:15 am

thanks so much

Post by ridwan »

tahnks for all your help it works gr8 now :wink:
Post Reply