PDA

View Full Version : Php MySql Displaying Data



designers_hub
10th April 2006, 11:26 AM
Hey,

i know how to display data as a list.

but how do you format it into a table where it goes:

3
3
3
3

etc?

purana
10th April 2006, 11:30 AM
Didnt find anything on google?

bergerac
10th April 2006, 11:36 AM
Im assuming you have your data in an array?

You can use an foreach loop which will create a new row for each piece of data eg

<table>
<?php foreach(&#036;yourarray as &#036;item) : ?>

<tr>
<td>
<?php echo &#036;item ?>
</td>
</tr>

<?php endforeach; ?>
</table>


The documentation at www.php.net (http://www.php.net) is your friend.

iSlayer
10th April 2006, 11:42 AM
can you post more of your code...

if your using sql i guess your probably use a while loop on the sql results ?

designers_hub
10th April 2006, 12:01 PM
&#60;?php
&#036;_SESSION&#91;&#39;connection&#39;&#93; = OK;
include &#39;cms/connect.php&#39;;
&#036;query = &#34;SELECT * FROM products WHERE public = &#39;YES&#39; ORDER BY rank ASC&#34;;
&#036;result = mysql_query&#40;&#036;query&#41;;
&#036;num = mysql_numrows&#40;&#036;result&#41;;

echo &#39;
&#60;table border=&#34;0&#34; cellspacing=&#34;0&#34; cellpadding=&#34;0&#34; width=&#34;786&#34;&#62;
&#60;tr&#62;
&#39;;

&#036;i=0;
while &#40;&#036;i &#60; &#036;num&#41; {

&#036;title = mysql_result&#40;&#036;result,&#036;i,&#34;title&#34;&#41;;
&#036;price = mysql_result&#40;&#036;result,&#036;i,&#34;price&#34;&#41;;
&#036;profit = mysql_result&#40;&#036;result,&#036;i,&#34;profit&#34;&#41;;
&#036;rrp = mysql_result&#40;&#036;result,&#036;i,&#34;rrp&#34;&#41;;
&#036;display = mysql_result&#40;&#036;result,&#036;i,&#34;display&#34;&#41;;
&#036;information = mysql_result&#40;&#036;result,&#036;i,&#34;information&#34;&#41;;
&#036;shipping = mysql_result&#40;&#036;result,&#036;i,&#34;zone1&#34;&#41;;
&#036;email = mysql_result&#40;&#036;result,&#036;i,&#34;email&#34;&#41;;

echo &#39;
&#60;td align=&#34;center&#34; valign=&#34;middle&#34; width=&#34;260&#34; height=&#34;350&#34; colspan=&#34;1&#34; bgcolor=&#34;#AFAFAF&#34;&#62;
&#60;table width=&#34;258&#34; height=&#34;348&#34; border=&#34;0&#34; cellspacing=&#34;0&#34; cellpadding=&#34;0&#34; bgcolor=&#34;#FFFFFF&#34;&#62;
&#60;tr&#62;
&#60;td align=&#34;center&#34; valign=&#34;middle&#34;&#62;
&#60;span class=&#34;producttitle&#34;&#62;&#39;; print &#036;title; echo&#39;&#60;/span&#62;&#60;br&#62;&#60;br&#62;
&#60;img src=&#34;&#39;; print &#036;display; echo&#39;&#34; width=&#34;200&#34; height=&#34;175&#34;&#62;&#60;br&#62;&#60;br&#62;
&#60;span class=&#34;ourprice&#34;&#62;OUR PRICE&#58; &#036;&#39;; print &#036;price; echo&#39;&#60;/span&#62;&#60;br&#62;
&#60;span class=&#34;profitmargin&#34;&#62;PROFIT MARGIN&#58; &#39;; print &#036;profit; echo&#39;%&#60;/span&#62;&#60;br&#62;
&#60;span class=&#34;rrp&#34;&#62;RRP&#58; &#036;&#39;; print &#036;rrp; echo&#39;&#60;/span&#62;&#60;br&#62;&#60;br&#62;
&#60;form target=&#34;paypal&#34; action=&#34;https&#58;//www.paypal.com/cgi-bin/webscr&#34; method=&#34;post&#34;&#62;
&#60;input type=&#34;hidden&#34; name=&#34;cmd&#34; value=&#34;_cart&#34;&#62;
&#60;input type=&#34;hidden&#34; name=&#34;business&#34; value=&#34;&#39;; print &#036;email; echo&#39;&#34;&#62;
&#60;input type=&#34;hidden&#34; name=&#34;item_name&#34; value=&#34;&#39;; print &#036;title; echo&#39;&#34;&#62;
&#60;input type=&#34;hidden&#34; name=&#34;item_number&#34; value=&#34;&#39;; print &#036;id; echo&#39;&#34;&#62;
&#60;input type=&#34;hidden&#34; name=&#34;amount&#34; value=&#34;&#39;; print &#036;price; echo&#39;&#34;&#62;
&#60;input type=&#34;hidden&#34; name=&#34;currency_code&#34; value=&#34;AUD&#34;&#62;
&#60;input type=&#34;hidden&#34; name=&#34;shipping&#34; value=&#34;&#39;; print &#036;shipping; echo&#39;&#34;&#62;
&#60;input type=&#34;image&#34; src=&#34;graphics/add.gif&#34; Border=&#34;0&#34; name=&#34;submit&#34;&#62;&#60;a href=&#34;&#39;; print &#036;information; echo&#39;&#34; target=&#34;_self&#34;&#62;&#60;img src=&#34;graphics/information.gif&#34; border=&#34;0&#34;&#62;&#60;/a&#62;
&#60;input type=&#34;hidden&#34; name=&#34;add&#34; value=&#34;1&#34;&#62;
&#60;/form&#62;
&#60;/td&#62;
&#60;/tr&#62;
&#60;/table&#62;
&#60;/td&#62;
&#60;td width=&#34;2&#34;&#62;&#60;/td&#62;
&#39;;

if &#40;&#036;i == 2&#41;
{
echo &#39;&#60;/tr&#62;
&#60;tr&#62;&#60;td height=&#34;2&#34;&#62;&#60;/td&#62;&#60;/tr&#62;
&#60;tr&#62;&#39;;
}
if &#40;&#036;i == 5&#41;
{
echo &#39;&#60;/tr&#62;
&#60;tr&#62;&#60;td height=&#34;2&#34;&#62;&#60;/td&#62;&#60;/tr&#62;
&#60;tr&#62;&#39;;
}
if &#40;&#036;i == 8&#41;
{
echo &#39;&#60;/tr&#62;
&#60;tr&#62;&#60;td height=&#34;2&#34;&#62;&#60;/td&#62;&#60;/tr&#62;
&#60;tr&#62;&#39;;
}
if &#40;&#036;i == 11&#41;
{
echo &#39;&#60;/tr&#62;
&#60;tr&#62;&#60;td height=&#34;2&#34;&#62;&#60;/td&#62;&#60;/tr&#62;
&#60;tr&#62;&#39;;
}
if &#40;&#036;i == 14&#41;
{
echo &#39;&#60;/tr&#62;
&#60;tr&#62;&#60;td height=&#34;2&#34;&#62;&#60;/td&#62;&#60;/tr&#62;
&#60;tr&#62;&#39;;
}
if &#40;&#036;i == 17&#41;
{
echo &#39;&#60;/tr&#62;
&#60;tr&#62;&#60;td height=&#34;2&#34;&#62;&#60;/td&#62;&#60;/tr&#62;
&#60;tr&#62;&#39;;
}
if &#40;&#036;i == 20&#41;
{
echo &#39;&#60;/tr&#62;
&#60;tr&#62;&#60;td height=&#34;2&#34;&#62;&#60;/td&#62;&#60;/tr&#62;
&#60;tr&#62;&#39;;
}


&#036;i++;
}


echo &#39;
&#60;/tr&#62;
&#60;/table&#62;
&#39;;

?&#62;


I have actually worked it out, but i need to loop this:



if &#40;&#036;i == 20&#41;
{
echo &#39;&#60;/tr&#62;
&#60;tr&#62;&#60;td height=&#34;2&#34;&#62;&#60;/td&#62;&#60;/tr&#62;
&#60;tr&#62;&#39;;
}


Basically it displays the data as:
3
3
3
3
3
etc

iSlayer
10th April 2006, 12:49 PM
your probably over complicating it a little



&#036;query = &#34;SELECT * FROM products WHERE public = &#39;YES&#39; ORDER BY rank ASC&#34;;
&#036;result = mysql_query&#40;&#036;query&#41;;
while &#40; &#036;row = mysq_fetch_array &#40; &#036;result &#41; &#41; {

}

that gives you an array with all the keys for each row of the database

what are you actually trying to end up wuith layout wise ?

designers_hub
10th April 2006, 04:03 PM
well each record is in a box, and i have 3 boxes per row, it&#39;s just to match my site layout.

bergerac
10th April 2006, 04:09 PM
So you would like it to be

3 3 3
3 3 3
3 3 3

instead of

3
3
3
3
3
3
3
3

designers_hub
10th April 2006, 11:18 PM
yeah sorry.

Record 1, Record 2, Record 3,
Record 4, Record 4, Record 5...

before i was saying 3 records, 3 records... misscommunication ;)

bergerac
10th April 2006, 11:30 PM
This should do it. Haven&#39;t tested it though so it might need some tweaking.


echo &#39;<table>&#39;;

for(&#036;i=0;&#036;i<count(&#036;yourarray); &#036;i++) {
echo &#39;<tr>&#39;;
for(&#036;c=0;&#036;c<3; &#036;c++) {
echo &#39;<td>&#39;.&#036;yourarray[&#036;i+&#036;c].&#39;</td>&#39;;

}
&#036;i = &#036;i + 3;
echo &#39;</tr>&#39;;
}

echo &#39;</table>&#39;;

BiRDBRAiN
11th April 2006, 07:26 AM
Depending on what you need, the only change to the above that I would make would be to stick
width="33%" inside the <td> tag to give the columns equal widths.

And maybe some extra parameters inside the <table> tag, e.g. <table width="100%" cellpadding="5" cellspacing="0" border="0">.

It&#39;s good especially for Internet Exploder which gives you some default values inside the <table> tags. :lol:

Another method could be to use the foreach() function if you have an array;

E.g.


function makeTable&#40;&#036;array,&#036;columns&#41;
{

# start of table
print &#40;&#34;&#60;table width=&#092;&#34;100%&#092;&#34; cellpadding=&#092;&#34;5&#092;&#34; cellspacing=&#092;&#34;0&#092;&#34; border=&#092;&#34;0&#092;&#34;&#62;&#34;&#41;;

&#036;counter = 1; // start a counter

&#036;columns = 3; // set number of columns
&#036;width = &#40;100/&#036;columns&#41; . &#34;%&#34;; // work out % width of columns

foreach&#40;&#036;array as &#036;value&#41;
{
if &#40;&#036;counter == 0&#41;
print &#40;&#34;&#60;tr&#62;&#34;&#41;; // start a new row

print &#40;&#34;&#60;td width=&#092;&#34;&#036;width&#092;&#34;&#62;&#34;&#41;;
print &#40;&#34;&#036;value&#34;&#41;; // output the value of this row in the array

if &#40;&#036;counter == &#036;columns&#41;
{
print &#40;&#34;&#60;/tr&#62;&#34;&#41;; // end the row if its this is the last column
&#036;counter = 1; // reset the counter
}
else
{
&#036;counter++; // increment the counter if this isn&#39;t the last column
}
}

# close the table
print &#40;&#34;&#60;/table&#62;&#34;&#41;;
}


The above would give you a way of changing the number of columns by using the variable &#036;columns. Note this would only work if you want a table that is 100% in width, i.e. if its sitting inside another table cell which it needs to fill.

To call it you would pass it the array and the number of columns

E.g.


&#036;array&#91;&#93; = &#40;&#34;a&#34;,&#34;b&#34;,&#34;c&#34;,&#34;d&#34;,&#34;e&#34;,&#34;f&#34;,&#34;g&#34;,&#34;h&#34;,&#34;i&#34;&#41;;

makeTable&#40;&#036;array,3&#41;; // give it &#036;array to use and make three columns

designers_hub
11th April 2006, 02:01 PM
BiRDBRAiN: WOW very clever&#33;

thanks, working like a charm.

iSlayer
11th April 2006, 02:04 PM
hey birdbrain 2 questions...
wouldnt that "fail" if the number of items is not divisable by 3 since the last </tr> tag would get left out and there would be missing colums...

and when you reset the counter shouldnt it be back to 0 not 1

you probably need to check the value of &#036;counter at the end of the loop and if its not &#036;0 then you would need to add filler columns and close the row

BiRDBRAiN
11th April 2006, 02:25 PM
Yep thats a good point about being divisible by three, should add in some extra code for that bit.

LOL I wrote that code above just after I got out of bed this morning :)

With the counter I figured I was easier to say blah == foo, rather than blah < foo-1, but yeah personal preference.

designers_hub
11th April 2006, 09:58 PM
not i just tried it just then, works with no problems what so ever, i did change it around a bit:



&#036;counter = 1;
&#036;columns = 3;
&#036;i=0;

echo &#39;
&#60;table border=&#34;0&#34; cellspacing=&#34;0&#34; cellpadding=&#34;0&#34;&#62;
&#60;tr&#62;
&#39;;

while &#40;&#036;i &#60; &#036;num&#41; {

MYSQL RECORD THINGY

if &#40;&#036;counter == &#036;columns&#41;
{
echo &#39;
&#60;/tr&#62;
&#60;tr&#62;
&#60;td height=&#34;2&#34;&#62;
&#60;/td&#62;
&#60;/tr&#62;
&#60;tr&#62;&#39;; // end the row if its this is the last column
&#036;counter = 1; // reset the counter
}
else
{
&#036;counter++; // increment the counter if this isn&#39;t the last column
}

&#036;i++;
}

echo &#39;
&#60;/tr&#62;
&#60;/table&#62;
&#39;;

BiRDBRAiN
12th April 2006, 12:14 PM
Just a quick update, I thought about your question iSlayer about looping through the array and what would happen if it had rows not divisible by the number of columns.

This wouldn&#39;t break it and you wouldn&#39;t need to add in a condition to check this because the counter automatically resets to one if it is equal to the number of columns and it would simply exit on the last row because I used a foreach() function, but what would happen is you would have to write some code to check how many columns were left because you would need to add some extra blank columns to make it generate a complete HTML table.

iSlayer
12th April 2006, 12:28 PM
yep thats it
"break" wasnt the right word :)

it still seems to me the counter should be reset to 0 otherwise you will only ever open one <tr> tag

so assuming its reset to 0 this code would add filler colums



if &#40; &#036;counter &#33;= 0 &#41; {
for &#40; &#036;x = &#036;counter; &#036;x &#60; &#036;columns; &#036;x++ &#41; {
echo &#34;&#60;td&#62;&#60;/td&#62;&#34;;
}
echo &#34;&#60;/tr&#62;&#34;;
}


or if you prefer while loops



if &#40; &#036;counter &#33;= 0 &#41; {
while &#40; &#036;counter &#60; &#036;columns &#41; {
echo &#34;&#60;td&#62;&#60;/td&#62;&#34;;
&#036;counter++;
}
echo &#34;&#60;/tr&#62;&#34;;
}