Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with arrays please.
Old 01-06-2006, 05:22 PM Help with arrays please.
Average Talker

Posts: 28
Location: Australia
Trades: 0
I have a DB with a number of telephone numbers for each person. I am using a while loop to extract and store them in an array. I then display the telephone numbers on the form.

The echo I have when storing the data displays “Home” and “Business” (correct for this person). However, when I display the data on the form all three fields contain “Array[0]”. What am I doing wrong?

This is the code I have so far:

PHP Code:
<?php
session_start
();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Web Template by Glyndwr D. Bartlett</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta content="CD Copyright © 4005 Glyndwr D. Bartlett" name="description">
<meta content="Glyndwr D. Bartlett" name="author"></head>
 
<?php
//Connect to the database.
$link=mysql_connect("127.0.0.1","root")
OR die(
"Couldn't connect to MySQL");
$db=mysql_select_db("db1",$link)
OR die(
"Couldn't open db1: " .mysql_error());
 
//Extract the Telephone numbers.
$sql_query4 sprintf("SELECT * FROM tbl_telephone_number
WHERE Name_Key = %s"
,
$_SESSION['NameKey']);
$result mysql_query($sql_query4);
$affected mysql_num_rows($result);
if (
$affected 0) {
session_unset();
echo 
"<font face='Verdana' size='2' color=red>Error in getting Name Key from tbl_telephone_number.<br>";
echo 
"<center> <input type='button' value='Retry' onClick='history.go(-1)'></center>";
exit();
}
else {
$i=0;
while (
$row mysql_fetch_array($result)){
$_SESSION[TelephoneType][$i] = $row['Telephone_Type'];
$_SESSION[TelephoneNumber][$i] = $row['Telephone_Number'];
$_SESSION[TelephoneExtension][$i] = $row['Telephone_Extension'];
echo 
$_SESSION[TelephoneType][$i];
$i++;
}
}
 
//Dissconnect from the database.
mysql_close($link);
?>
 
<form class="form" name="UpdateDetails" action="CheckUpdate.php" method="POST">
<table cellspacing="1" cellpadding="1" width="100%" border="0"> 
<tr>
<td bgcolor="#cccccc" colspan="3">
<div align="center">Please enter your details below: </div></td></tr>
<tr>
<td bgcolor="#cccccc" colspan="3">
<div align="left">The fields indicated with an asterisk (*) are required to complete the transaction; 
other fields are optional. If you do not want to provide us with the required information, 
please use the back button on your browser, or close the window or browser session that is 
displaying this page, to return to the previous page.</div></td></tr>
 
<tr>
<td width="40%">
<div align="right">
<p class="small">*Telephone number 1: </p></div></td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">*Type: </p></div></td>
<td>
<?php
unset($sel_Telephone1Type);
unset(
$sel_Telephone1Type2);
$sel_State[$_POST[Telephone1Type]] = "Home";
echo 
"
<select name=Telephone1Type>
<option value='
$_SESSION[TelephoneType][0]'>$_SESSION[TelephoneType][0]</option>
<option 
{$sel_Telephone1Type[val1]} value='Home'>Home</option>
<option 
{$sel_Telephone1Type[val2]} value='Business'>Business</option>
<option 
{$sel_Telephone1Type[val3]} value='Mobile'>Mobile</option>
<option 
{$sel_Telephone1Type[val4]} value='Business Facimile'>Business Facimile</option> 
<option 
{$sel_Telephone1Type[val5]} value='Home Facimile'>Home Facimile</option> 
</select>
"
;
?> 
</td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">*Telephone number: </p></div></td>
<td><input class="box2" id="TelephoneNumber1" maxlength="25" size="15" name="TelephoneNumber1" value="<?PHP echo "$_SESSION[TelephoneNumber][0]" ?>"></td>
<td width="1%">
<div align="left">
<p class="small">Ext: </p></div>
<td><input class="box2" id="Extension1" maxlength="5" size="5" name="Extension1" value="<?PHP echo "$_SESSION[TelephoneExtension][0]" ?>"></td></td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Telephone number 2: </p></div></td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Type: </p></div></td>
<td>
<?php
unset($sel_Telephone2Type);
unset(
$sel_Telephone2Type2);
$sel_State[$_POST[Telephone2Type]] = "Home";
echo 
"
<select name=Telephone2Type>
<option value='
$TelephoneType[1]'>$TelephoneType[1]</option>
<option 
{$sel_Telephone2Type[val1]} value='Home'>Home</option>
<option 
{$sel_Telephone2Type[val2]} value='Business'>Business</option>
<option 
{$sel_Telephone2Type[val3]} value='Mobile'>Mobile</option>
<option 
{$sel_Telephone2Type[val4]} value='Business Facimile'>Business Facimile</option> 
<option 
{$sel_Telephone2Type[val5]} value='Home Facimile'>Home Facimile</option> 
</select>
"
;
?> 
</td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Telephone number: </p></div></td>
<td><input class="box2" id="TelephoneNumber2" maxlength="25" size="15" name="TelephoneNumber2" value="<?PHP echo "$TelephoneNumber[1]?>"></td>
<td width="1%">
<div align="left">
<p class="small">Ext: </p></div>
<td><input class="box2" id="Extension2" maxlength="5" size="5" name="Extension2" value="<?PHP echo "$TelephoneExtension[1]?>"></td></td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Telephone number 3: </p></div></td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Type: </p></div></td>
<td>
<?php
unset($sel_Telephone3Type);
unset(
$sel_Telephone3Type2);
$sel_State[$_POST[Telephone3Type]] = "Home";
echo 
"
<select name=Telephone3Type>
<option value='
$TelephoneType[2]'>$TelephoneType[2]</option>
<option 
{$sel_Telephone3Type[val1]} value='Home'>Home</option>
<option 
{$sel_Telephone3Type[val2]} value='Business'>Business</option>
<option 
{$sel_Telephone3Type[val3]} value='Mobile'>Mobile</option>
<option 
{$sel_Telephone3Type[val4]} value='Business Facimile'>Business Facimile</option> 
<option 
{$sel_Telephone3Type[val5]} value='Home Facimile'>Home Facimile</option> 
</select>
"
;
?> 
</td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Telephone number: </p></div></td>
<td><input class="box2" id="TelephoneNumber3" maxlength="25" size="15" name="TelephoneNumber3" value="<?PHP echo "$TelephoneNumber[2]?>"></td>
<td width="1%">
<div align="left">
<p class="small">Ext: </p></div>
<td><input class="box2" id="Extension3" maxlength="5" size="5" name="Extension3" value="<?PHP echo "$TelephoneExtension[2]?>"></td></td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Telephone number 4: </p></div></td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Type: </p></div></td>
<td>
<?php
unset($sel_Telephone4Type);
unset(
$sel_Telephone4Type2);
$sel_State[$_POST[Telephone4Type]] = "Home";
echo 
"
<select name=Telephone4Type>
<option value='
$TelephoneType[3]'>$TelephoneType[3]</option>
<option 
{$sel_Telephone4Type[val1]} value='Home'>Home</option>
<option 
{$sel_Telephone4Type[val2]} value='Business'>Business</option>
<option 
{$sel_Telephone4Type[val3]} value='Mobile'>Mobile</option>
<option 
{$sel_Telephone4Type[val4]} value='Business Facimile'>Business Facimile</option> 
<option 
{$sel_Telephone4Type[val5]} value='Home Facimile'>Home Facimile</option> 
</select>
"
;
?> 
</td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Telephone number: </p></div></td>
<td><input class="box2" id="TelephoneNumber4" maxlength="25" size="15" name="TelephoneNumber4" value="<?PHP echo "$TelephoneNumber[3]?>"></td>
<td width="1%">
<div align="left">
<p class="small">Ext: </p></div>
<td><input class="box2" id="Extension4" maxlength="5" size="5" name="Extension4" value="<?PHP echo "$TelephoneExtension[3]?>"></td></td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Telephone number 5: </p></div></td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Type: </p></div></td>
<td>
<?php
unset($sel_Telephone5Type);
unset(
$sel_Telephone5Type2);
$sel_State[$_POST[Telephone5Type]] = "Home";
echo 
"
<select name=Telephone5Type>
<option value='
$TelephoneType[4]'>$TelephoneType[4]</option>
<option 
{$sel_Telephone5Type[val1]} value='Home'>Home</option>
<option 
{$sel_Telephone5Type[val2]} value='Business'>Business</option>
<option 
{$sel_Telephone5Type[val3]} value='Mobile'>Mobile</option>
<option 
{$sel_Telephone5Type[val4]} value='Business Facimile'>Business Facimile</option> 
<option 
{$sel_Telephone5Type[val5]} value='Home Facimile'>Home Facimile</option> 
</select>
"
;
?> 
</td></tr>
<tr>
<td width="40%">
<div align="right">
<p class="small">Telephone number: </p></div></td>
<td><input class="box2" id="TelephoneNumber5" maxlength="25" size="15" name="TelephoneNumber5" value="<?PHP echo "$TelephoneNumber[4]?>"></td>
<td width="1%">
<div align="left">
<p class="small">Ext: </p></div>
<td><input class="box2" id="Extension5" maxlength="5" size="5" name="Extension5" value="<?PHP echo "$TelephoneExtension[4]?>"></td></td></tr> 
 
<td colspan="3">
<div align="center">
<input id="Submit" type="image" height="14" alt="Submit" width="57" src="images/login_03.gif" border="0" name="Submit">
&nbsp;&nbsp; 
<input id="Reset" type="reset" height="14" alt="Reset Your Info" width="57" src="images/reset_03.gif" border="0" name="Reset"> 
</div></td></tr>
<tr>
<td colspan="3" height="3"></td></tr>
<tr bgcolor="#cccccc">
<td colspan="3">
<div align="center">
<a class="link1" href="Join/Index.php" target="_top">
<font color="#000000">Link to something.</font></a> 
<br>
<tr bgcolor="#cccccc">
<td colspan="3" height="3"></td></tr>
</table>
</form>
</html>


Your assistance would be greatly appreciated,

Glyn
GlyndwrBartlett is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-07-2006, 12:01 AM
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
In the line of code just above
PHP Code:
<select name=Telephone1Type
, type in
PHP Code:
print_r($_SESSION[TelephoneType]); 
and tell us what you see.
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 01-07-2006, 05:17 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
PHP Code:
if ($affected 0) { 
^This is going to cause a problem, you are assigning 0 to $affecte. It needs to be:
PHP Code:
if ($affected == 0) { 
if you want to test for equality.

Also, TelephoneType should really be in quotes : $_SESSION['TelephoneType'];
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 01-07-2006, 08:50 PM
Average Talker

Posts: 28
Location: Australia
Trades: 0
Thanks Oberon and fambi,

The "if =" is a common mistake I make and I will have to watch.

When I put in the line of code I get "Array([1] => Business)".

Thanks for your help,

Glyn
GlyndwrBartlett is offline
Reply With Quote
View Public Profile
 
Old 01-08-2006, 12:15 AM
fambi's Avatar
Ultra Talker

Posts: 339
Trades: 0
I'm not sure if you've changed the code, but the reason why i asked you to do that was because i thought "Array" was printing out because the value of $_SESSION[TelephoneType] was an array.

To get the word 'business' to show at that point you would need $_SESSION[TelephoneType][1], but i can't seem to find where this goes in your post. Sorry to be of no help!
__________________

Please login or register to view this content. Registration is FREE
from a
Please login or register to view this content. Registration is FREE
is easy!
Read this
Please login or register to view this content. Registration is FREE
that uses our
Please login or register to view this content. Registration is FREE
.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 01-08-2006, 02:12 AM
Average Talker

Posts: 28
Location: Australia
Trades: 0
Hi fambi,

Sorry I had changed the code trying to fix it. I returned the code to its original state and got the result "Array([0]=>Home[1]=>Business).

I have changed the code to:
PHP Code:
<?php
unset($sel_Telephone1Type);
unset(
$sel_Telephone1Type2);
$sel_State[$_POST[Telephone1Type]] = "Home";
$TelephoneType1 $_SESSION[TelephoneType][0];
echo 
"
<select name=Telephone1Type>
<option value=
$TelephoneType1 >$TelephoneType1 </option>
<option 
{$sel_Telephone1Type[val1]} value='Home'>Home</option>
<option 
{$sel_Telephone1Type[val2]} value='Business'>Business</option>
<option 
{$sel_Telephone1Type[val3]} value='Mobile'>Mobile</option>
<option 
{$sel_Telephone1Type[val4]} value='Business Facimile'>Business Facimile</option> 
<option 
{$sel_Telephone1Type[val5]} value='Home Facimile'>Home Facimile</option> 
</select>
"
;
?>
And it now works. Must be something to do with the <option>?

Thanks for all your help,

Glyn
GlyndwrBartlett is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help with arrays please.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17221 seconds with 12 queries