retro Posted May 11, 2008 Share Posted May 11, 2008 I currently have a 'report' page which displays the results of a query in columns, such as: NameItem 1Item 2Date Joe Bloggs1010/05/08 Fred Smith0210/05/08 These results will be orders on a particular day. I am using the following code: // print table header echo("<table cellspacing=\"0\" cellpadding=\"0\" width=\"500\" class=\"edittable\"> <tr><th>Name</th><th>Item 1</th><th>Item 2</th><th>Date</th></tr>"); // fetch the current row into the array $row while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo("<tr>"); echo("<td>" . $row["FirstName"] . " " . $row["Lastname"] . "</td>"); echo("<td>" . $row["Item1"] . "</td>"); echo("<td>" . $row["Item2"] . "</td>"); echo("<td>" . $row["date"] . "</td>"); echo "</tr>"; } echo("</table>"); However, what I want to output is what will look more like a delivery sheet, as follows: Name: Joe Bloggs Item 1: 1 Item 2: 0 Date: 10/05/08 Name: Fred Smith Item 1: 0 Item 2: 2 Date: 10/05/08 How can I go about doing this? Link to comment https://forums.phpfreaks.com/topic/105123-formatting-a-report/ Share on other sites More sharing options...
ady01 Posted May 11, 2008 Share Posted May 11, 2008 This works for me, you will need to amend for the amount of columns though - think this is what you want to do <?php include 'config.php' //your config or connection file //set 3 to 4 of you want 4 columns. Set it to 5 if you want 5, etc $numcols = 3; $numcolsprinted = 0; // no of columns so far $query = "SELECT * FROM table"; //amend table name for your database $mysql_result = mysql_query($query, $db); while($myrow = mysql_fetch_row($mysql_result)) $tn = $myrow[0]; $in= $myrow[1]; //keep adding for further rows if ($numcolsprinted == $numcols) { print "</tr>\n<tr>\n"; $numcolsprinted = 0; } echo "<td>$in $tn</td>\n"; $numcolsprinted++; } $colstobalance = $numcols - $numcolsprinted; for ($i=1; $i<=$colstobalance; $i++) { } print "<TD></TD>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/105123-formatting-a-report/#findComment-538206 Share on other sites More sharing options...
Btown2 Posted May 11, 2008 Share Posted May 11, 2008 <html> <body> <table> <tr><td>Name:</td><td>/*firstname stuff goes here*/</td></tr> <tr><td>Item 1:</td><td>/*item 1 stuff here*/</td></tr> <tr><td>Item 2:</td><td>/*item 2 stuff*/</td></tr> <tr><td>Date:</td><td>/*date stuff goes here*/</td></tr> </table> </body> </html> this will get you the format you wanted. Link to comment https://forums.phpfreaks.com/topic/105123-formatting-a-report/#findComment-538207 Share on other sites More sharing options...
retro Posted May 11, 2008 Author Share Posted May 11, 2008 Btown2: Thanks, but I'm OK with the html formatting. Its more the looping to get the data into the table that I'm having problems with! ady01: Thanks for that! I tried it pretty much as-is, changing the database / table info, but I got an error. It said there is an unexpected } - it is the one that appears after $numcolsprinted++; I tried removing the bracket, and all I got was 11! I'm not sure what's going on there! I put the bracket back, and removed the previous one, and there was no output at all! Is this pseudo code, or complete? I notice there's no <table>. I'm not sure whether this is going to do what I want. I'll have a play with it. I notice you're using mysql_fetch_row where I used mysql_fetch_array - I guess this will make a difference, so I'll look into using that. Thanks! One thing I didn't mention - I'd rather have the items as seperate tables. So if you look at my example, it would print one table for Joe Bloggs' order, then print a new table underneath (but with the same layout) for Fred Smith's order. If anyone has any more ideas, I'd love to hear them! Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/105123-formatting-a-report/#findComment-538216 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.