Select box help

  CutNpaste 14:52 03 Jan 2007

i wanted to display dynamic data in a selectbox from two or three different database tables.
How do i do that???
LAnguage: php & mysql

  LeadingMNMs 18:21 03 Jan 2007

$result = mysql_query("SELECT col1, col2 FROM table1, table2");
$rows = mysql_numrows($result);

if($rows !== 0)
$i = 0;

echo "<p>Label: <select name=\"something\" >";

while($i < $rows)
echo "<option value=\"". mysql_result($result, $i, "column") . "\">" . mysql_result($result, $i, "col2") . "</option>";

echo "</select>";
// What to do if the query has no rows

Would be the simple way I'd do it, but there are many different functions to interface with MySQL.

  CutNpaste 23:19 03 Jan 2007

hey thanks,
great help!
by the way how do i display information both by table column and rows.

cheers thanx

  LeadingMNMs 08:00 04 Jan 2007

Not entirely sure what you mean by that, can you elaborate on what you are trying to achieve ?

  CutNpaste 11:53 04 Jan 2007

ok what i meant was that i want to display pictures and text stored in my database to a web page dnamically in a table. But i want to display 3 columns of pictures by 3 rows.

i hope im making sense here.

  LeadingMNMs 18:33 04 Jan 2007

Ok, so if I understand you correctly you want to 3x3 grid with a picture in each cell, and you have a MySQL table that contains the URLs, or at the simplest the filenames of the pictures you want to display.

Essentially the process is the same, with the HTML output modified to create the table and put an image within each cell. Obviously you need to consider what happens if you don't have 9 image filenames stored in the database, although if you gurantee that will always be the case then I guess you can forget about it. However if it is possible there may be no rows, or perhaps say only 8, it will need to be handled. Either by not creating the table at all, or ensuring the correct the HTML table still has the correct number of rows and columns.

So as above, do your query to get the filename or url of the pictures. Check the rows and decide if your actually going to draw the table. If you are then:

echo "<table><tr>"

while($i < 9) // Only do 9 cells
echo "<td><img src=\"path/". mysql_result($result, $i, "url") . "\">" . "</td>"; // Depending how the picture name is stored, you may or may not need the "path"
if($i % 3 == 0) // Every 3 items create a new row (not entirely sure if modulus is valid in PHP
echo "</tr><tr>";

echo "</table>";

  CutNpaste 19:30 04 Jan 2007

ok im gonna try that cheers!!

This thread is now locked and can not be replied to.

Elsewhere on IDG sites

Alienware 17 R4 2017 review

These brilliant Lego posters show just what children's imaginations are capable of

Mac power user tips and hidden tricks

Comment réinitialiser votre PC, ordinateur portable ou tablette Windows ?