Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help needed with arrays

Status
Not open for further replies.

lentildal

Programmer
Sep 24, 2001
25
GB
hey folks

i have a table with these fields in each row...

commentid (autoincrement int key)
commentname (text)
commentmail (text)
commentsite (text)
commentcomment (text)
commentdte (timestamp)

i have this code...

$query = "SELECT commentid, commentname, commentmail, commentsite, commentcomment, commentdte FROM $table_name ORDER BY commentid DESC";
$res = mysql_query($query);

// build the display array

while($row = @mysql_fetch_array($res)) {
$disp_array[$row[commentid]] = $row[commentname];
}


then this to display...


<form>
<?
while(list($id,$comment) = @each($disp_array)) { ?>
<input name=&quot;name[<?= $id ?>]&quot; size=&quot;30&quot; maxlength=&quot;70&quot; value=&quot;<?= $comment ?>&quot;>
<?}?>
</form>


what this does is create four input textboxes

(there are four entries in the table)

what i want to be able to do is create other textboxes to include all the other info for each row of my table. thing is i cant work out how the arrays are supposed to go

can anyone help?

thanks

give me all your lentils
 
why not build the HTML when fetching the values from the database?

To mantain your code, you can always do this:
while($row = @mysql_fetch_array($res)) {
$disp_array[$row[commentid]] = array(name=>$row[commentname],mail=>$row[commentemail],...);
}


while (list($id,$array)=each($disp_array)){
// here $id have the id, and $array[name] the name, $array the email, and so on.
}

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Try making an array of arrays, like this:
Code:
while ($row = mysql_fetch_assoc($res)) {
  $disp_array[$row[&quot;commentid&quot;]] = $row;
}
Then in the section where you display your textboxes, use this:
Code:
while (list($id,$comment)=each($disp_array)) {
  while (list($varname,$varval)=each($comment)) {
    echo &quot;<input&quot;;
    echo &quot; name=\&quot;&quot;.$varname.&quot;[&quot;.$id.&quot;]\&quot;&quot;;
    echo &quot; size=\&quot;30\&quot;&quot;;
    echo &quot; maxlength=\&quot;70\&quot;&quot;;
    echo &quot; value=\&quot;&quot;.$varval.&quot;\&quot;&quot;;
    echo &quot;>&quot;;
  }
}
However, an alternative would be to combine these steps and avoid the use of
Code:
$disp_array
altogether - unless you need it somewhere else.

You can of course, pass similar 'array of arrays' through your textboxes - it all depends how you want the data structured on your receiving script.

-Rob
 
hi guys thanks for your replies

hi again anikin

i tried your code out and i had it working for a second, but then i added more textboxes and i cant make it work anymore. this is what i have so far:


$query = &quot;SELECT * FROM $table_name ORDER BY commentid DESC&quot;;
$res = mysql_query($query);

// build the display array
$comment = array(name=>$row[commentname],mail=>$row[commentmail],site=>$row[commentsite],comm=>$row[commentcomment],dte=>$row[commentdte]);
while($row = @mysql_fetch_array($res)) {
$disp_array[$row[commentid]] = $comment;
}
?>

<div>
<table cellspacing='1' cellpadding='0' border='0' bgcolor='#ff0000'>
<form method='POST' action=&quot;<?= $PHP_SELF ?>&quot;>

<tr><td>Comments</td><td>Delete</td></tr>
<?
while(list($id,$comment) = @each($disp_array)) { ?>
<tr><td>Date added:<?= $array[dte] ?></td><td align='center' rowspan='5'><input type=&quot;checkbox&quot; name=&quot;delete[<?= $id ?>]&quot; value=&quot;yes&quot;></td></tr>
<tr><td><input name=&quot;name[<?= $id ?>]&quot; size=&quot;30&quot; maxlength=&quot;70&quot; value=&quot;<?= $comment[name] ?>&quot;></td></tr>
<tr><td><input name=&quot;name[<?= $id ?>]&quot; size=&quot;30&quot; maxlength=&quot;70&quot; value=&quot;<?= $comment[mail] ?>&quot;></td></tr>
<tr><td><input name=&quot;name[<?= $id ?>]&quot; size=&quot;30&quot; maxlength=&quot;70&quot; value=&quot;<?= $comment[site] ?>&quot;></td></tr>
<tr><td><TEXTAREA NAME=&quot;name[<?= $id ?>]&quot; ROWS=4 COLS=26 WRAP=&quot;virtual&quot;><?= urlencode($comment[comm]) ?></TEXTAREA></td></tr>
<? } ?>
<tr><td> </td><td align='center' height='35'><input type='image' src='images/gobutt.gif' name='sub' border='0'></td></tr></form></table></div>


do you know what i am doing wrong?

thankyou
give me all your lentils
 
This line is INSIDE the while loop:
$comment = array(name=>$row[commentname],mail=>$row[commentmail],site=>$row[commentsite],comm=>$row[commentcomment],dte=>$row[commentdte]);

before this:
$disp_array[$row[commentid]] = $comment;

and this:
<?= $array[dte] ?>
should be:
<?= $comment[dte] ?>

everything else looks fine, but tell if this works.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
hmmm

how can i make the URLs printout properly?

i tried adding rulencode but that doesnt do it properly either...
give me all your lentils
 
For that you have the rawurlencode and rawurldecode functions. you must test them. I don't have time for testing that now.

but, what's in the database? the url should be a string. Probably it's already wrong in the DB. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
no the urls are links in the database. i made them into links because they are also displayed in a flash textbox which can read html.

the guestbook is in flash. when someone submits their email or URL it gets turned into a link before it is saved to the database. its done with preg_replace. i got the script from the php manual but i dont understand it well enough yet to reverse it.
give me all your lentils
 
so don't do it the first place. You know it's a link so let it be a link in the database.

Don't do treatment to the data when inserting in the Database unless it's required to.

If some one enters the address as someone@somewhere.world save it unchanged in the database.

When retriving, instead of :
echo $email;
do:
echo &quot;<a href=\&quot;mailto:$email\&quot;>$email</a>&quot;;

With this you save trouble, and some bytes in storage.

An example when you need to process data before the database. When the user enters an URL. He can enter an HTTP address, an FTP address or even an HTTP address without the http://

Here you should process the string to see if it is started by http:// or ftp://, if so you leave it unchanged, else you push http:// to the begining of the URL (supponsint that unspecified is http). Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
hi

i think you are right anikin, that is a much better way of doing it. i tried the mailto thing and it works well, in flash too. but how do you write the code to check for http:// and turn it into a link?

thanks
give me all your lentils
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top