jeger003 Posted February 10, 2009 Share Posted February 10, 2009 hello everyone, I am trying to create a table to display some items but when i use the while() function to display the table it displays it all vertically and i need it to display horizontally.....here is my code below basically i want it like this image image image price price price repeating this way -----> title title title include "db.php"; $query = mysql_query("SELECT price, title FROM listings") or die(mysql_error()); echo "<table>"; while($fetch = mysql_fetch_array($query)) { echo "<tr>"; echo "<td>test1</td>"; echo "</tr>"; echo "<tr>"; echo "<td>test2</td>"; echo "</tr>"; echo "<tr>"; echo "<td>test3</td>"; echo "</tr>"; } echo "</table>"; Link to comment https://forums.phpfreaks.com/topic/144556-solved-i-need-table-to-display-content-horizontally-from-mysql-in-php/ Share on other sites More sharing options...
mrdamien Posted February 10, 2009 Share Posted February 10, 2009 include "db.php"; $query = mysql_query("SELECT price, title FROM listings") or die(mysql_error()); $images = $prices = $titles = array(); while($fetch = mysql_fetch_array($query)) { $images[] = "<td>test1</td>"; $prices[] = "<td>test2</td>"; $titles[] = "<td>test3</td>"; } echo "<table>"; echo "<tr>" . implode("", $images) . "</tr>"; echo "<tr>" . implode("", $prices) . "</tr>"; echo "<tr>" . implode("", $titles) . "</tr>"; echo "</table>"; Link to comment https://forums.phpfreaks.com/topic/144556-solved-i-need-table-to-display-content-horizontally-from-mysql-in-php/#findComment-758706 Share on other sites More sharing options...
jeger003 Posted February 10, 2009 Author Share Posted February 10, 2009 include "db.php"; $query = mysql_query("SELECT price, title FROM listings") or die(mysql_error()); $images = $prices = $titles = array(); while($fetch = mysql_fetch_array($query)) { $images[] = "<td>test1</td>"; $prices[] = "<td>test2</td>"; $titles[] = "<td>test3</td>"; } echo "<table>"; echo "<tr>" . implode("", $images) . "</tr>"; echo "<tr>" . implode("", $prices) . "</tr>"; echo "<tr>" . implode("", $titles) . "</tr>"; echo "</table>"; whoa awesome work! now i need to call values from the database and im not really sure how to do that this is what i tried with the code your provided: include "db.php"; $query = mysql_query("SELECT listings_images_urls.classified_id, listings_images_urls.thumb_url, listings.title, listings.image, listings.viewed FROM listings_images_urls, listings WHERE image = 1 AND listings.id = listings_images_urls.classified_id ORDER BY listings.viewed DESC LIMIT 1,5 ") or die(mysql_error()); $images = $prices = $titles = array(); while($fetch = mysql_fetch_array($query)) { $images['thumb_url'] = "<td>'".$images['thumb_url']."' id '".$images['id']."'</td>"; $prices['price'] = "<td>'".$prices['price']."' id '".$prices['id']."'</td>"; $titles['title'] = "<td>'".$titles['title']."'</td>"; } echo "<table>"; echo "<tr>" . implode("", $images) . "</tr>"; echo "<tr>" . implode("", $prices) . "</tr>"; echo "<tr>" . implode("", $titles) . "</tr>"; echo "</table>"; it doesnt work. it displays this ' id ''' id ''' id ''' id ''' id ''' id ''' id ''' id '''''' ' ' ' ' '' id '' ' ' ' ' '' id '' ' ' ' ' '' i would really appreciate if you or anyone can explain a little with whats going in the code you provided....i'd like to be able to use it in the future Link to comment https://forums.phpfreaks.com/topic/144556-solved-i-need-table-to-display-content-horizontally-from-mysql-in-php/#findComment-759094 Share on other sites More sharing options...
jeger003 Posted February 10, 2009 Author Share Posted February 10, 2009 Oh got it!! Thanks for the help mrdamien!!!! now the last thing i would like is if anyone can just explain a little what this code is doing....i look it up on php.net but its not as easy to understand.....maybe just explain the terminology a little include "db.php"; $query = mysql_query("SELECT price, title FROM listings") or die(mysql_error()); $images = $prices = $titles = array(); while($fetch = mysql_fetch_array($query)) { $images[] = "<td>test1</td>"; $prices[] = "<td>test2</td>"; $titles[] = "<td>test3</td>"; } echo "<table>"; echo "<tr>" . implode("", $images) . "</tr>"; echo "<tr>" . implode("", $prices) . "</tr>"; echo "<tr>" . implode("", $titles) . "</tr>"; echo "</table>"; like what does this mean? why are they equaled to each other then to an array? $images = $prices = $titles = array(); why did you put [] after the variables $image,$prices,$title? $images[] = "<td>test1</td>"; $prices[] = "<td>test2</td>"; $titles[] = "<td>test3</td>"; last question is...what is implode doing? again, i have looked them up.....maybe not explain it all but a piece would be nice i would really appreciate it thanks everyone Link to comment https://forums.phpfreaks.com/topic/144556-solved-i-need-table-to-display-content-horizontally-from-mysql-in-php/#findComment-759128 Share on other sites More sharing options...
mrdamien Posted February 11, 2009 Share Posted February 11, 2009 like what does this mean? why are they equaled to each other then to an array? $images = $prices = $titles = array(); This is just to initialize variables. It could also be done like this: $images = array(); $prices = array(); $titles = array(); or not at all. why did you put [] after the variables $image,$prices,$title? $images[] = "<td>test1</td>"; $prices[] = "<td>test2</td>"; $titles[] = "<td>test3</td>"; $array[] = is equivalent to array_push. last question is...what is implode doing? Implode takes an array, and gives you a string of all the values, seperated by the first paramaters value. EX: Implode array(1,2,3) with '#' gives "1#2#3" Link to comment https://forums.phpfreaks.com/topic/144556-solved-i-need-table-to-display-content-horizontally-from-mysql-in-php/#findComment-759505 Share on other sites More sharing options...
jeger003 Posted February 12, 2009 Author Share Posted February 12, 2009 oh this is cool stuff! thanks so much for the help mrdamien I really appreciate it. Link to comment https://forums.phpfreaks.com/topic/144556-solved-i-need-table-to-display-content-horizontally-from-mysql-in-php/#findComment-760619 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.