display only 3 foreach result per row - php

i have script as follows
$output="<table class='products'><tr>";
while($info = mysql_fetch_array( $data )) {
//Outputs the image and other data
$output.= "<td>
<img src=http://localhost/zack/sqlphotostore/images/" .$info['photo'] ." width=323px ></img>
<b>Name:</b> ".$info['name'] . "
<b>Email:</b> ".$info['email'] . "
<b>Phone:</b> ".$info['phone'] . "</td> ";
}
$output.="<tr></table>";
print $output;
?>
it shows all results in long horizontal line how do i break the results so that they show
in new row after 3 count.

Keep a counter, and output a new table row every 3 images
$output="<table class='products'>";
$counter = 0;
while($info = mysql_fetch_array( $data ))
{
if( $counter % 3 == 0 )
$output .= '<tr>';
$output .= "<td>";
$output .= "<img src=http://localhost/zack/sqlphotostore/images/".$info['photo'] ." width=323px ></img>";
$output .= "<b>Name:</b> ".$info['name'];
$output .= "<b>Email:</b> ".$info['email'];
$output .= "<b>Phone:</b> ".$info['phone']."</td> ";
if( $counter % 3 == 0)
$output .= "</tr>";
$counter++;
}
$output.="</table>";
print $output;
?>

Add a counter and start a new row every time it reaches a multiple of 3.
$counter = 0;
while($info = mysql_fetch_array($data)) {
if ($counter++ % 3 == 0) {
if ($counter > 0) {
$output .= "</tr>";
}
$output .= "<tr>";
}
// stuff
}
if ($counter > 0) {
$output .= "</tr>";
}
$output .= "</table>";
Please note: It may not help answer your question, but you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.

Related

PHP While Loops Table Mysqli Results

I'm about month and a half into PHP. I tried to make function for fetching results from find_all_subjects():
function find_all_subjects() {
global $dbconnect;
$q = "SELECT * FROM subjects ORDER BY id ASC";
$subject_set = mysqli_query($dbconnect, $q);
confirm_query($subject_set);
return $subject_set;
}
and then making a new function which will loop the result in table rows. The problem is it loops just one result... when I did it with <ul> and <li> it worked fine but it doesn't seem to work when doing it with the table. My table core tags are in another document. So it's not that...
function navigacija() {
$s = find_all_subjects();
while($subjects = mysqli_fetch_assoc($s)) {
$out = "<tr>";
// Ime Teme
$out .= "<td width='25%' height='40'> <a href=\"admin_content.php?subject=" . urlencode($subjects['id']) . "\">";
$out .= $subjects['menu_name'];
$out .= "</a></td>";
// Vidljiva
if($subjects['visible'] == 1) {
$subjects['visible'] = 'DA';
} else {
$subjects['visible'] = 'NE';
}
$out .= "<td width='25%' height='40'><p align=\"center\">DA / NE";
$out .= "</p></td>";
// Broj Strana
$out .= "<td width='25%' height='40'>";
$pages_set = find_sub_from_pages($subjects['id']);
while($pages = mysqli_fetch_assoc($pages_set)) {
$out .= "" . $pages['menu_name'] . "";
$out .= "</td>";
}
// Vidljiva
$out .= "<td align=\"center\" width='25%' height='40'>";
$out .= "<img width=\"17px\" height=\"17px\" src=\"st/img/ic-arup.png\">
</img> <img width=\"17px\" height=\"17px\" src=\"st/img/ic-ardown.png\"></img> ";
$out .= "</td>";
$out .= "</tr>";
}
return $out;
}
the problem is that you reset
$out = "<tr>";
every time in the loop.
change this line to
$out .= "<tr>";
and only put declaration out of the loop
$out = "";

Put for loops inside a variable?

In all my years of writing PHP, I've never come across an occasion were I would need to put for loops inside a standard php variable.
The reason: I need to pass this variable through a JSON request.
See my current code below.
What I'm doing here is writing a script to generate a standard HTML table based on users requirements (e.g number of rows & columns). And I need to put all this HTML into a variable and pass the variable through a JSON request which then decodes and is displayed to the user.
Any advice/tips/tricks would be a huge help.
<?php
$trows = 5;
$tcolumns = 7;
echo "<table class='table table-striped table-bordered'>";
echo "<thead>";
echo "<tr>";
for ($th = 0; $th < $tcolumns; $th++){echo '<th>[HEADER]</th>';
};
echo "</tr>";
echo "</thead>";
echo "<tbody>";
for ($tr = 0; $tr < $trows; $tr++){ echo '<tr>';
for ($td = 0; $td < $tcolumns; $td++){echo '<td>[CONTENT]</td>';
};
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
<?php
$trows = 5;
$tcolumns = 7;
$result = "";
$result .= "<table class='table table-striped table-bordered'>";
$result .= "<thead>";
$result .= "<tr>";
for ($th = 0; $th < $tcolumns; $th++){$result .= '<th>[HEADER]</th>';
};
$result .= "</tr>";
$result .= "</thead>";
$result .= "<tbody>";
for ($tr = 0; $tr < $trows; $tr++){$result .= '<tr>';
for ($td = 0; $td < $tcolumns; $td++){$result .= '<td>[CONTENT]</td>';
};
$result .= "</tr>";
}
$result .= "</tbody>";
$result .= "</table>";
?>
Create a variable, say $output, to store the html table in.
After you have finished building the table you can do whatever you choose with it. Print it out, use it in another variable to build a json object.
See below
$output = "<table class='table table-striped table-bordered'>";
$output .= "<thead>";
$output .= "<tr>";
for ($th = 0; $th < $tcolumns; $th++){
$output .= '<th>[HEADER]</th>';
};
$output .= "</tr>";
$output .= "</thead>";
$output .= "<tbody>";
for ($tr = 0; $tr < $trows; $tr++){
$output .= '<tr>';
for ($td = 0; $td < $tcolumns; $td++){
$output .= '<td>[CONTENT]</td>';
};
$output .= "</tr>";
}
$output .= "</tbody>";
$output .= "</table>";
echo $output;
Use output buffering:
// Start output buffering
ob_start();
/*
Your code here
*/
// Fetch buffered output and save to variable
$content = ob_get_contents();
// End output buffering, flush buffer. This outputs the buffer content
ob_end_clean();
// If you don't want the buffer to be output use this
// ob_end_clean();

Displaying an array of data in to a table of 4 columns

I have a php array which contains 45 items that I would displayed into a table of 4 columns. The code below is what I have.
$output = '<table border="1">';
for($tr=0; $tr<=count($question_answers); $tr++)
{
$output .= "<tr>";
for($td=1; $td<=$cols; $td++){
$output .= "<td>" . $question_answers[$tr] . "</td>";
}
$output .= "</tr>";
}
$output .= "</table>";
How do I split that data so that it split into 4 columns? Any ideas?
$output = '<table border="1">';
for($tr=0; $tr<=count($question_answers); $tr++)
{
$output .= "<tr>";
for($td=1; $td<=$cols; $td++){
$output .= "<td>" . $question_answers[$tr] . "</td>".($tr % 4 == 0 ? "</tr><tr>" : "");
}
$output .= "</tr>";
}
$output .= "</table>";
Or for this case: "What I am trying to achieve a table with a maximum of 12 rows and 4 columns. The first column would be from 1 to 12, second column 13 - 24, third column 25 - 36 and fourth column 37 etc... "
$output = '<table border="1">';
for($tr=0; $tr<12; $tr++){
$output .= "<tr>";
for($td=0; $td<=3; $td++){
if(!empty($question_answers[$td*12+$tr]))
$output .= "<td>" .$question_answers[$td*12+$tr] . "</td>";
}
$output .= "</tr>";
}
$output .= "</table>";
The result would be:
http://joxi.ru/E2pbzzvf8Xp8rY

How can I print out <tr> after every 4 mysql entries in my while loop?

$q2 = mysql_query("SELECT * FROM artwork LEFT JOIN folder ON folder.folder_id=artwork.folder_id WHERE id IN(0".$userids.")");
while($row = mysql_fetch_array($q2)){
$link .= '<td align="center"><a href="/art/'.$row['id'].'" title="'.$row['name'].'">
<img src="/img/artwork/'.$row['folder'].'/'.$row['file'].'" height="80" /></a><br />
<span align="center">[remove] [view]</span></td>';
}
link is getting echo'd out with one td, but I need to make it so after every 4 mysql entries it adds a tr
Just maintain a counter and test if it is divisible by 4:
$counter = 0;
while ($row = mysql_fetch_array($q2)) {
if ($counter%4 === 0) $link .= "<tr>";
$link .= "<td>...</td>";
if ($counter%4 === 3) $link .= "</tr>";
$counter++;
}
Define a variable to count td's and check if it's rminder by 4 is 0 then open an close tr
$item = 0;
while ($row = mysql_fetch_array($q2)) {
if ($item % 4 == 0) $link .= "<tr>";
$link .= "<td>...</td>";
if ($item % 4 == 0) $link .= "</tr>";
$item++;
}
if($item % 4 != 0){//IF COUNT OF TD IN LAST ROW IS LESS THAN 4
while($item % 4 != 0){
$link.= "<td></td>";
$item++;
}
$link.= "</tr>";
}

Display a table in while loop

I am trying to display a table in while loop with 3 rows and 3 td for each row. But I am confusing how to archived thit.
This is my code so far:
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output = "<table>\n";
$output .= " <tr>\n";
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
$output .= " </tr>\n";
$output .= "</table>\n";
echo $output;
}
The above code given me a table with multiple rows and 1 td for each row. But it is not my expecting result.
Can anybody tell me how can I display such a table in my While loop?
The current code creates a new table for every single entry. What you want is to move the <table> and </table> pieces outside of the loop, then you want a counter to keep track of how many cells you've handled. If it's even, start a new <tr>. Otherwise, end the current </tr>. Make sure you check at the end to see if you've finished a </tr>, and optionally add an empty <td></td> if needed.
this aught to do it
<?php
echo "<table>\n";
$cells = $total = 0;
$total_cells = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
$testimonial = nl2br($row['testimonial']);
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
if ($cells === 0)
echo "<tr>\n";
//Create new testimonial output
$output = " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
$cells++;
$total++;
if ($cells === 3 || $total === $total_cells)
{
echo "</tr>\n";
$cells = 0;
}
}
echo "</table>\n";
?>
Try this
$i = 0;
echo "<table>\n<tr>";
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
if( $i %2 == 1 )
echo "</tr><tr>\n";
$i++;
}
echo "</tr>\n</table>\n";
EDIT
In general, for displaying n cells per row, change the if statement to
if( $i % n == (n - 1) )

Categories