Jump to content
xisto Community
farsiscript

Display MySQL results in a Table question

Recommended Posts

hi all
i know php and my sql
i want to know how can show mysql result in this tabel for example :

<table cellpadding="0" cellspacing="0" width="200" height="151">	<!-- MSTableType="layout" -->	<tr>		<td height="21" width="100" valign="top">1</td>		<td height="21" width="100" valign="top">8</td>	</tr>	<tr>		<td height="21" width="100" valign="top">2</td>		<td height="21" width="100" valign="top">9</td>	</tr>	<tr>		<td height="21" width="100" valign="top">3</td>		<td height="21" width="100" valign="top">10</td>	</tr>	<tr>		<td height="22" width="100" valign="top">4</td>		<td height="22" width="100" valign="top">11</td>	</tr>	<tr>		<td height="22" width="100" valign="top">5</td>		<td height="22" width="100" valign="top">12</td>	</tr>	<tr>		<td height="22" width="100" valign="top">6</td>		<td height="22" width="100" valign="top">13</td>	</tr>	<tr>		<td height="22" width="100" valign="top">7</td>		<td height="22" width="100" valign="top">14</td>	</tr></table>

this table show result in 2 columns .
Is it imposible to show result in this tabale with 1 query ?
Just paste query here
thanks all

Share this post


Link to post
Share on other sites

It might be more possible, if you changed the order that it is sorted in. For example, instead of it counting going down a column like in your example. It would count going from left to right. And you would keep track of it. Whenever it retrieves two results, you tell the script to add a new row, and then reset the "tracker". And have it loop until no more results are found.But since i have not fully thought through this, a problem would result if the tracker doesn't reach the limit. Cause then it won't end the row.Anyways, i hope i gave you a general idea on how to go about it.

Share this post


Link to post
Share on other sites

Yes it is possible i think. I did find a decent tutorial on this but i cant find it any more. Basically you get the data from the sql query into an array. I assume you know how to do this, if not see the link at the end.

Basically you create the array from the output of the query then use a loop to execurte code like:

echo "<tr><td>$array[result];</td><td>$array[result];</td>";

obviously thats not suitable code to use but it shows the idea. A better tutorial can be found here

http://www.desilva.biz/arrays/poparray.html

And also all over the web using google to search for something like "php mysql array" or search the function "mysql_fetch_array()" i belive thats correct, its been a while scince i did any database work.

Share this post


Link to post
Share on other sites

Humm if i remember back to when i learnt it the code was something like this.

Lets pretend that the table users contains two fields, username and password and one user is called joe.

$sql = "SELECT * FROM users WHERE username=joe";$result = $msql_query($sql, $link);$row = mysql_fetch_array($result);// now we have the data in our array called $row so we can use echo ""'; to pull it out and print it. echo "<TR><TD>$row['username']</TD><TD>$row[password]</TD>";


I appologise i misunderstood before and the link i gave you wasnt very usefull although its jogged my memory so if for example you wanted to pull out several rows of data from your table such as every user living in england something like this should work assuming the table has three fields, country, username and password.

$sql = "SELECT * FROM users WHERE country=england";$result = $msql_query($sql, $link);while ($row = mysql_fetch__array($result) {//get data into array then use it untill there are no more rows to retrieve.echo "<TR><TD>$row['username']</TD><TD>$row[password]</TD></TR>";}
Thats probably not entirely correct as im not an expert and its been a while but it gives a good idea of what is needed and should be fairly accurate. Basically you get the data from the table using the query. You then set up a loop that says "while there are still rows for me to get im going to get them then create a HTML table with these results. When theres none left ill stop." So if there were three records in the database who live in england you should get three table rows in the HTML output each with a different users details. Thats the plan atleast!

Like i said try looking up the mysql_fetch_array() function and it should help.

This is the site where i learnt this from and i belive it should be exactly what youre after.
http://www.tizag.com/mysqlTutorial/mysqlselect.php

Share this post


Link to post
Share on other sites

This is good if you have a small page.
Well you would get a error if there were no results from the query due to either a wrong query or No results in the table.

Therefore your mysql_fetch_array() would give you an error.
Hence you must check the $reults first or the mysql_num_rows() to see if there are any results.

Check it as follows:

if(!$result || mysql_num_rows($result) < 1){	  //Give the error (God knows what you want)	  return;}

Also i prefer to use the for loop instead of while.
So do what pleases you.

One last comment is that you should always keep the processing file and the theme file seperate.
It makes it easier to understand and creates an insulation between the two.

Share this post


Link to post
Share on other sites

oh thanks dear electron

its nice help

 

i have one question

can i show one row details in 2 td ?

for example i change it :

 

$sql = "SELECT * FROM users WHERE username=joe";$result = $msql_query($sql, $link);$row = mysql_fetch_array($result);// now we have the data in our array called $row so we can use echo ""'; to pull it out and print it. echo "<TR><TD>$row['username']</TD><TD>$row['username']</TD>";

?

can i show in 2 td username field ?

thanks more and more

 

 

for more

its my sql file and mysql database code :

CREATE TABLE host (id tinyint(4) NOT NULL auto_increment,name text NOT NULL,PRIMARY KEY (id)) TYPE=MyISAM;

its my show.php code :

<?phpinclude "config.php";echo "<table cellpadding='0' cellspacing='0' width='154'>	<!-- MSTableType='layout' -->";$sql = "select * FROM host order by id";$result = $msql_query($sql, $link);while($row = mysql_fetch_array($result));{echo "<tr><td height='29' width='74'>$row['name']</td><td height='29' width='80'>$row['name']</td></tr>";}echo "</table>";?>

I want show next name at table

for example in td1 show name one and in td show name 2 in database and goes to end

for example if in my database i have 2 name farsi and script

i want show in tabale in this format

<tr><td height='29' width='74'>farsi</td><td height='29' width='80'>script</td></tr>

and

<tr><td height='29' width='74'>3 name</td><td height='29' width='80'>4name</td></tr>

<tr><td height='29' width='74'>5 name</td><td height='29' width='80'>to end of tabel</td></tr>

 

dear if you can edit my show.php code , plz help me thanks and more thanks <_<

Edited by farsiscript (see edit history)

Share this post


Link to post
Share on other sites

I think the only way to get it to show two usernames in one table is to change the php like this:

echo "<TR><TD>$row['username']</TD>";

Then loop that line somehow.

Otherwise you will get the same username twice.

Share this post


Link to post
Share on other sites

wooow
i make it :D
its my query and tabale :

$getpet = "1";$level1 = "1";$level2 = "2";$level3 = "3";$level4 = "4";$level5 = "5";$max_results = 20;$sql = mysql_query("SELECT * FROM host ORDER BY rate DESC LIMIT $from, $max_results"); while($row = mysql_fetch_array($sql)){ $name = $row['name'];$rate = $row['rate'];if ($getpet == "$level1"){echo "<tr><td height='19' width='96'><font face='Tahoma' style='font-size: 8pt'><font color='#800000'>$num .</font><a title='$rate' href='send.php?nam=$name'><font color='#808000'><span style='text-decoration: none'>$name</span></font></a></font></td>";$getpet = "2";$num = ($num + 1);}elseif ($getpet == "$level2"){echo "<td height='19' width='97'><font face='Tahoma' style='font-size: 8pt'><font color='#800000'>$num .</font><a title='$rate' href='send.php?nam=$name'><font color='#808000'><span style='text-decoration: none'>$name</span></font></a></font></td>";$getpet = "3";$num = ($num + 1);}elseif ($getpet == "$level3"){echo "<td height='19' width='97'><font face='Tahoma' style='font-size: 8pt'><font color='#800000'>$num .</font><a title='$rate' href='send.php?nam=$name'><font color='#808000'><span style='text-decoration: none'>$name</span></font></a></font></td>";$getpet = "4";$num = ($num + 1);}elseif ($getpet == "$level4"){echo "<td height='19' width='97'><font face='Tahoma' style='font-size: 8pt'><font color='#800000'>$num .</font><a title='$rate' href='send.php?nam=$name'><font color='#808000'><span style='text-decoration: none'>$name</span></font></a></font></td>";$getpet = "5";$num = ($num + 1);}elseif ($getpet == "$level5"){echo "<td height='19' width='97'><font face='Tahoma' style='font-size: 8pt'><font color='#800000'>$num .</font><a title='$rate' href='send.php?nam=$name'><font color='#808000'><span style='text-decoration: none'>$name</span></font></a></font></td></tr>";$getpet = "1";$num = ($num + 1);}

Share this post


Link to post
Share on other sites

wooowi make it :D
its my query and tabale :

$getpet = "1";$level1 = "1";$level2 = "2";$level3 = "3";$level4 = "4";$level5 = "5";$max_results = 20;$sql = mysql_query("SELECT * FROM host ORDER BY rate DESC LIMIT $from, $max_results"); while($row = mysql_fetch_array($sql)){ $name = $row['name'];$rate = $row['rate'];if ($getpet == "$level1"){echo "<tr><td height='19' width='96'><font face='Tahoma' style='font-size: 8pt'><font color='#800000'>$num .</font><a title='$rate' href='send.php?nam=$name'><font color='#808000'><span style='text-decoration: none'>$name</span></font></a></font></td>";$getpet = "2";$num = ($num + 1);}elseif ($getpet == "$level2"){echo "<td height='19' width='97'><font face='Tahoma' style='font-size: 8pt'><font color='#800000'>$num .</font><a title='$rate' href='send.php?nam=$name'><font color='#808000'><span style='text-decoration: none'>$name</span></font></a></font></td>";$getpet = "3";$num = ($num + 1);}elseif ($getpet == "$level3"){echo "<td height='19' width='97'><font face='Tahoma' style='font-size: 8pt'><font color='#800000'>$num .</font><a title='$rate' href='send.php?nam=$name'><font color='#808000'><span style='text-decoration: none'>$name</span></font></a></font></td>";$getpet = "4";$num = ($num + 1);}elseif ($getpet == "$level4"){echo "<td height='19' width='97'><font face='Tahoma' style='font-size: 8pt'><font color='#800000'>$num .</font><a title='$rate' href='send.php?nam=$name'><font color='#808000'><span style='text-decoration: none'>$name</span></font></a></font></td>";$getpet = "5";$num = ($num + 1);}elseif ($getpet == "$level5"){echo "<td height='19' width='97'><font face='Tahoma' style='font-size: 8pt'><font color='#800000'>$num .</font><a title='$rate' href='send.php?nam=$name'><font color='#808000'><span style='text-decoration: none'>$name</span></font></a></font></td></tr>";$getpet = "1";$num = ($num + 1);}


i use a code like this:
$result = mysql_query("SELECT naam FROM Evenement WHERE $date2 >'begindatum'",$link);
$row = mysql_fetch_array($result);
$i = 0;
while ($i<= mysql_num_rows($result))
{
print("Getal: ".$i. "<br>");

print ("<option>$row[$i]</option>");
$i++;
}

if i want to print out another $row in the same option( in my case) i would do it like this
$result = mysql_query("SELECT naam FROM Evenement WHERE $date2 >'begindatum'",$link);
$row = mysql_fetch_array($result);
$i = 0;
while ($i<= mysql_num_rows($result))
{
print("Getal: ".$i. "<br>");

print ("<option>$row[$i]");
$i++;
print("<option>$row[$i]</option>");
$i++;
}

but at the moment i get this error: Notice: Undefined offset: 1 in


i hope this will help you

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.