Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How to pull multi-dimensional array from database and display in 4 columns & 8 rows

How to pull multi-dimensional array from database and display in 4 columns & 8 rows
Thread Tools
Fresh-Faced Recruit
Join Date: Aug 2006
Status: Offline
Reply With Quote
Jun 2, 2006, 04:20 PM
 
How to pull multi-dimensional array from database and display in 4 columns & 8 rows? I have been stressing over the display. I have 30 records in the database and want to display the items into 4 columns and 8 rows. I have tried changing codes many times and sometimes, it will display only one record or only the first row(4 records). Can someone please tell me what went wrong?

Here is my code:
================================================== =
$conn = mysql_connect("localhost","user","password")or die(mysql_error());
mysql_select_db("database",$conn);
$query="SELECT P_ID, StNo, Street, City, PropertyType, Bedrooms, Bathrooms, AskPrice FROM properties";
$rs = mysql_query($query, $conn) or die(mysql_error());
$i= mysql_num_rows($rs);
if ($i<1)
{
$display="<p> You have no property </p>";

}
else
{
echo "$i";
$display="<table width='60%' border='1'>";

$w=4;
$j=0;
$row = array();
while ($resultrow = mysql_fetch_assoc($rs))
{
$row[] = $resultrow;

while (($w<30)&& ($i>0))
{ $display.="<tr>";
while ( ($j < $w) &&($w<30))
{
print_r($row[$j]);

$pid=$row[$j]['P_ID'];
$stno=$row[$j]['StNo'];
$stname=$row[$j]['Street'];
$city=$row[$j]['City'];
$propertytype=$row[$j]['PropertyType'];
$bedrooms=$row[$j]['Bedrooms'];
$bathrooms=$row[$j]['Bathrooms'];
$askprice=$row[$j]['AskPrice'];
$display.="<br>Row [$j] [PID]: $pid";
$display.="<td>$pid <br>
$stno $stname, $askprice<br>
$city</td>";
echo "<br>Inside the loop<br>j: $j";
echo "i with j: $i";
$j++;
} $display.="</tr>";
$w= $w+4;
}

}
$display.="</table>";
mysql_free_result($rs);
mysql_close($conn);
}

==============the end=======================================
     
Dedicated MacNNer
Join Date: Nov 2001
Location: Are Eye
Status: Offline
Reply With Quote
Jun 2, 2006, 10:27 PM
 
I like to grab the array first so I know what I'm working with.

so something like

$results = array();
while ($resultrow = mysql_fetch_assoc($rs)) {
$results[] = $resultrow;
}

then, $results is an array of all the junk that goes in the table.
start the table
<table>
<tr>
start a for loop
for ($i=0; $i<count($results); $i++) {
if($i%4) { //that means if $i can be divided evenly by 4
echo "</tr><tr>";
}
echo "<td>" . $results[$i] . "</td>";
}
then close up the table
</tr></table>

you'll need to put that into real code, but that's the gist of it. print_r($results) is a handy way of seeing your array to make sure you have the proper result set.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:50 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2