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) )
Related
How do I make a NxM table where each cell has two rows and its second row has two columns, but by using rowspan or colspan? I want to make something like this:
Here is my attempt.
Artikal.php:
public function get_html()
{
$html = "";
$html .= "<td>";
if (isset($this->sifra) && isset($this->naziv) && isset($this->cena) && isset($this->kolicina)) {
if ($this->kolicina > 0) {
$html .= "<img src=\"images/" . str_replace(" ", "-", $this->naziv) . jpg\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">{$this->sifra}</span>";
$html .= "<span class=\"cena\">{$this->cena} DIN</span>";
} else {
$html .= "<img src=\"images/none.png\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">{$this->sifra}</span>";
$html .= "<span class=\"cena\">{$this->cena} DIN</span>";
}
} else {
$html .= "<img src=\"images/none.jpg\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">X</span>";
$html .= "<span class=\"cena\">X</span>";
}
$html .= "</td>";
return $html;
}
index.php
if (isset($artikli)) {
for ($i = 0; $i < 6; $i++) {
echo "<tr>";
for ($j = 0; $j < 9; $j++)
echo $artikli[$i * 9 + $j]->get_html();
echo "</tr>";
}
}
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 = "";
function find_image_by_id() {
global $connection;
$query = "SELECT * ";
$query .= "FROM images ";
$query .= "WHERE page_id={$_GET["page"]}";
$image_set = mysqli_query($connection, $query);
confirm_query($image_set);
return $image_set;
}
function display_image_by_id(){
$current_image = find_image_by_id();
while($image=mysqli_fetch_assoc($current_image)){
$output = "<div class=\"images\">";
$output .= "<img src=\"images/";
$output .= $image["ilink"];
$output .= "\" width=\"72\" height=\"72\" />";
$output .= $image["phone_name"];
$output .= "</div><br />";
}
mysqli_free_result($current_image);
return $output;
}
This is the code I'm using to show the images stored as links in mysql
and the images are in a folder. But what happens after this code is executed only the second
value is displayed. I want both value/ images to be displayed.
Try something like that-
All you need to do is just initialize this variable outside the loop.
$output =''; //initialize before
SO your function look like this -
function display_image_by_id(){
$current_image = find_image_by_id();
$output =''; //initialize before
while($image=mysqli_fetch_assoc($current_image)){
$output .= "<div class=\"images\">";
$output .= "<img src=\"images/";
$output .= $image["ilink"];
$output .= "\" width=\"72\" height=\"72\" />";
$output .= $image["phone_name"];
$output .= "</div><br />";
}
mysqli_free_result($current_image);
return $output;
}
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.
I have a foreach loop like this
$sn_count = 1;
$html = '';
foreach($points as $point){
$html .= "<div class=\"points\">"
. "<div class=\"serial\">".$sn_count."</div>"
. "<div class=\"pointsdesc\">"
. $point['points_description']
. "</div></div>";
$sn_count++;
}
echo $html;
This code output is like this
1
First point description
2
second point description
etc...
Now i want to display ads after every 2,4,6,... points. I mean in-between 2-3,4-5,6-7,...
Can anyone help me with the code?
Thanks
You'll want to use the modulus
$sn_count = 1;
$html = '';
$display_ad_every = 3;
foreach($points as $point){
$html .= "<div class=\"points\">"
. "<div class=\"serial\">".$sn_count."</div>"
. "<div class=\"pointsdesc\">"
. $point['points_description']
. "</div></div>";
if($sn_count++ % $display_ad_every == 0)
{
// append ad code here
}
}
echo $html;
This should do it
$sn_count = 1;
$html = '';
foreach($points as $point){
$html .= "<div class=\"points\">"
. "<div class=\"serial\">".$sn_count."</div>"
. "<div class=\"pointsdesc\">"
. $point['points_description']
. "</div></div>";
if($sn_count % 2 === 0){
$html .= get_ad_code()
}
$sn_count++;
}
echo $html;