This is a discussion on Newbie question: MySQL, PHP with <a href>+$QUERY_STRING within the PHP General forums, part of the PHP Programming Forums category; Hi, I'm quite a newbie to PHP and MySQL. I'ld like to list a MySQL table with PHP ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
I'm quite a newbie to PHP and MySQL. I'ld like to list a MySQL table with PHP to a table on my webpage and then add hyperlinks to every element. It works fine with simple hyperlinks but I'ld like to add a $QUERY_STRING to my hyperlinks, and this string should be the contents of the MySQL cell that the listed rows contain. So, in theory, I've got a db row like this: id | product | type ---------------------- 1 | magazine | softcover 2 | book | softcover and so on. I'ld like to do this as a result: <a href="testcode.php?1">Magazine</a> and so on... For this, I made the following code: $query = "SELECT product FROM prods WHERE type = '$QUERY_STRING' ORDER BY id"; $result = mysql_query($query) or die("Query failed : " . mysql_error()); $numrows = mysql_num_rows ( $result); function identify ($ident) { $ident = mysql_query ("SELECT id FROM prods") or die("Query failed : " .. mysql_error()); $ident2 = mysql_fetch_array($ident); $ident = $ident2["id"]; //return $ident; } print "We have $numrows rows in the table.<P>"; print "<table width=\"100%\" border=0 cellspacing=\"0\" cellpadding=\"0\">\n"; while ( $one_line = mysql_fetch_row( $result) ) { print "<tr>\n"; foreach ( $one_line as $cell ) $ident = identify(); print "\t<td width=\"25%\" height=\"40\" valign=\"top\"><div align=\"justify\"><font size=\"2\" face=\"Arial, Helvetica, sans-serif\"><a href=\"testcode.php?$ident\">$cell</a></font></div></td>\n"; print "</tr>\n"; } print "</table>\n"; mysql_close($link); ?> I think I can handle the problem with creating the function identify(), but have problems where to put it and how to use it. It is possible that the solution is quite obvious but I can't figure it out. Can anyone please help me with this? Please send a copy to szabolcs@mediastar.hu as well. Thanks a lot: Szabolcs J. Csorge, szabolcs@mediastar.hu Budapest, Hungary |
|
|||
|
What about this one?
<? $query = "SELECT * FROM prods WHERE type = '$QUERY_STRING' ORDER BY id"; $result = mysql_query($query) or die("Query failed : " . mysql_error()); $numrows = mysql_num_rows ( $result); print "We have $numrows rows in the table.<P>"; print "<table width=\"100%\" border=0 cellspacing=\"0\" cellpadding=\"0\">\n"; while ( $one_line = mysql_fetch_array( $result) ) { print (<<<EOF <td width="25%" height="40" valign="top"> <div align="justify"> <font size="2" face="Arial, Helvetica,sans-serif"> <a href="testcode.php?id={$one_line['id']}">{$one_line['product']}</a> </font> </div> </td> EOF ); } print "</table>\n"; mysql_close($link); ?> ?> Alekc (al3kc at yah00 dot it) szabolcs@mediastar.hu (Szabolcs J. Csorge) wrote in message news:<60881450.0406101034.5ad6adb4@posting.google. com>... > Hi, > > > I'm quite a newbie to PHP and MySQL. I'ld like to list a MySQL table > with PHP to a table on my webpage and then add hyperlinks to every > element. It works fine with simple hyperlinks but I'ld like to add a > $QUERY_STRING to my hyperlinks, and this string should be the contents > of the MySQL cell that the listed rows contain. > > So, in theory, I've got a db row like this: > > id | product | type > ---------------------- > 1 | magazine | softcover > 2 | book | softcover > > and so on. I'ld like to do this as a result: > > <a href="testcode.php?1">Magazine</a> and so on... > > > For this, I made the following code: > > $query = "SELECT product FROM prods WHERE type = '$QUERY_STRING' ORDER > BY id"; > > $result = mysql_query($query) or die("Query failed : " . > mysql_error()); > $numrows = mysql_num_rows ( $result); > > function identify ($ident) > { > $ident = mysql_query ("SELECT id FROM prods") or die("Query failed : " > . mysql_error()); > $ident2 = mysql_fetch_array($ident); > $ident = $ident2["id"]; > //return $ident; > } > > print "We have $numrows rows in the table.<P>"; > > print "<table width=\"100%\" border=0 cellspacing=\"0\" > cellpadding=\"0\">\n"; > while ( $one_line = mysql_fetch_row( $result) ) > { > print "<tr>\n"; > foreach ( $one_line as $cell ) > $ident = identify(); > print "\t<td width=\"25%\" height=\"40\" valign=\"top\"><div > align=\"justify\"><font size=\"2\" face=\"Arial, Helvetica, > sans-serif\"><a href=\"testcode.php?$ident\">$cell</a></font></div></td>\n"; > print "</tr>\n"; > } > print "</table>\n"; > > mysql_close($link); ?> > > > I think I can handle the problem with creating the function > identify(), but have problems where to put it and how to use it. It is > possible that the solution is quite obvious but I can't figure it out. > > > Can anyone please help me with this? Please send a copy to > szabolcs@mediastar.hu as well. > > > Thanks a lot: > > > Szabolcs J. Csorge, > szabolcs@mediastar.hu > > Budapest, Hungary |