Jump to content

[SOLVED] i need table to display content horizontally from mysql in php


jeger003

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.