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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying mysql select results in a table accross the page 1

Status
Not open for further replies.

nichols

Technical User
May 24, 2001
92
GB
I want to be able to display the results of a mysql select query accross the page As seen below:

[record1] [record2][record3][record4]
[record5] [record6][record7][record8]
[record9] [record10][record11][record12]

I also want them to appear as hyperlinks to sub items under these records.

Does anyone know how to do this. I can only seem to find solutions which display the results in a column.

I am a newbie though so this may be a fairly easy solution.

Hope someone can help?
 
Code:
$result = mysql_query("select something from sometable");
$numCols = 4;  //change as you wish
$numRows = ceil(mysql_num_rows($result)/$numCols);
$table = "<table>\r\n";
for ($i=0; $i< $numRows; $i++):
 $table .= "\t<tr>\r\n";
 for ($r=0; $r<$numCols; $r++):
   if ( ($row=mysql_fetch_assoc($result)) === false):
    $table .= "\t\t<td>&nbsp;</td>\r\n"; 
   else:
    $table .= "\t\t<td>".$row['something']."</td>\r\n";
   endif; 
 endfor;
 $table .= "\t</tr>\r\n";
endfor;
$table .= "</table>";
 
Cheers for that that that is fantastic.

I know I'm pushing it but do you know how to make the record that is displayed a hyperlink.

What I mean by this is if the above table returns a category how can I display this as a hyperlink which will in activate another mysql select statement.

I don't know whether this is possible or not?
 
Code:
$result = mysql_query("select somethingID, somthingText from sometable");
$numCols = 4;  //change as you wish
$numRows = ceil(mysql_num_rows($result)/$numCols);
$linkpage = "[URL unfurl="true"]http://www.domain.com/example.php?something=";[/URL]

$table = "<table>\r\n";
for ($i=0; $i< $numRows; $i++):
 $table .= "\t<tr>\r\n";
 for ($r=0; $r<$numCols; $r++):
   if ( ($row=mysql_fetch_assoc($result)) === false):
    $table .= "\t\t<td>&nbsp;</td>\r\n";
   else:
    $table .= "\t\t<td><a href=\"$linkpage{$row['somethingID']}\">{$row['somethingText']}</a></td>\r\n";
   endif;
 endfor;
 $table .= "\t</tr>\r\n";
endfor;
$table .= "</table>";
 
Cheers for that it works perfectly.

Do you know any good resources i.e. books or tutorials will help me further my knowledge in php/mysql?

Thanks for all your help.

Chris
 
sorry, no.

other than... the php manual and this forum. download open source code and reverse engineer its purpose etc. the learning curve for php is very shallow.

i started my php learning curve with an o'reilly book, but it was so trivialised that i gave it up. others will have had different experiences of course.

on the above - the solution uses tables to guarantee four across. if you would rather use css you could do this

Code:
$result = mysql_query("select somethingID, somthingText from sometable");
$numCols = 4;  //change as you wish
$width = floor((100-($numCols))/$numcols);
$linkpage = "[URL unfurl="true"]http://www.domain.com/example.php?something=";[/URL]

$output = "<div>\r\n";
for ($i=0; $i< $numRows; $i++):
 for ($r=0; $r<$numCols; $r++):
   if ( ($row=mysql_fetch_assoc($result)) === false):
    $output .= "\t\t<span class=\"cell\">&nbsp;</span>\r\n";
   else:
    $output .= "\t\t<span class=\"cell\"><a href=\"$linkpage{$row['somethingID']}\">{$row['somethingText']}</a></span>\r\n";
   endif;
 endfor;
endfor;
$output .= "</div>";
echo <<<EOL

<style>
.cell { width:{$width}%; float:left; border:1px solid black; min-width:150px;}
</style>
$output
EOL;

using the above, as the page is made smaller the cells should resize too. under a certain pixel count (150) the boxes will start reshuffling into 3 cols etc. i'm not great at css so may have goofed the above, but you get the idea!
 
Cheers for that I will give it a try. Once again thanks for all your help on this your a star.

Cheers
Chris
 
The book I learned with was "PHP and MySQL Web Development, Third Ed." by Welling and Thomson. This books sits next to my desk for any quick PHP examples I may need. It includes a Cd Rom which has many examples which are great for learning from. I did have a programming background though so that made it a little easier.
 
Cheers for that I will get a copy. I need all the help I can get.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top