how to make a table row contents to break lline? [duplicate] - php

I have the following code:
for($i=0; $i<count($gallery);$i++)
{
$temp = array();
$temp = $gallery[$i];
echo "<img src='". $temp->path . "' />";
}
Now this code prints the content in one row. I want to print only 3 per row and then create new row and print another 3 and so on. How can this be done?
appreciate the support :)
EDIT: error
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 5
Filename: views/profile.php
Line Number: 105
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/profile.php
Line Number: 106

You can do
$n = 3;
echo "<table><tr>";
for($i=0; $i<count($gallery);$i++){
$temp = array();
$temp = $gallery[$i];
echo "<td><img src='". $temp->path . "' /></td>";
if($i % $n ==0 && $i!=0 ){
echo "</tr><tr>";
}
}
echo '</tr></table>';
Edit:
If you want to do it the "right" way - by building the syntactically correct HTML, you need to do:
$n = 3;
echo "<table><tr>";
$gallery_count = count($gallery);
for($i=0; $i<$gallery_count; $i++){
$temp = array();
$temp = $gallery[$i];
echo "<td><img src='". $temp->path . "' /></td>";
if($i != 0){
if($i % $n == 0 && $i != $gallery_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
}
//example - if the last 2 `td`s are missing:
$padding_tds = $gallery_count % $n;
if($padding_tds != 0 ){
$k = 0;
while($k < $padding_tds){
echo "<td> </td>";
}
}
echo '</tr></table>';

You just need to a modulus that checks how many have been printed - any multiple of 3 will add a break.
$x=0;
for($i=0; $i<count($gallery);$i++)
{
$x++;
$temp = array();
$temp = $gallery[$i];
echo "<img src='". $temp->path . "' />";
if (($x%3)==0) { echo "<br />"; }
}

I just redid it with tables, it's much neater, because each thing will be formatted to look correctly.
It's kind of messy because I just added a little if statement to release the tables.
<table>
<?php
$number_per_row = 3;
for($i=0; $i<count($gallery);$i++)
{
$temp = array();
$temp = $gallery[$i];
if(($i % $number_per_row) == 0) {
echo "<tr>";
}
?>
<td><?php echo "<img src='". $temp->path . "' />"; ?></td>
<?php
if(($i % $number_per_row) == $number_per_row - 1) {
echo "</tr>";
}
}
?>
</table>

$n = 3;
echo "<table>";
echo "<tr>";
for($i=0; $i<count($gallery);$i++){
if(($i % n ==0) && ($i != 0)){
echo "</tr><tr>";
}
$temp = array();
$temp = $gallery[$i];
echo "<td><img src='". $temp->path . "' /></td>";
}
echo "</tr>";
echo '</table>';``

Related

Count row and repeat the same rows within Loop

I'm working on mansory gallery here images fetching into the row first row contains three images and then for the second row contains two images and so on.
Now currently all images coming inside same row. I want to add in a loop I had tried but unable to achieve the result. For inspiration link
<?php
include('admin/config.php');
$result = mysqli_query($db, "SELECT * FROM gallery order by id desc");
// var_dump($result->num_rows);
while ($row = mysqli_fetch_array($result)) {
echo "<div class='gallery-items'>";
echo "<div class='mansory-item'>";
echo "<a href='admin/images/".$row['path']."' data-lightbox='gallery' class='ansa-thumb'>";
echo "<img src='admin/images/".$row['path']."' class='item-img img-1'>";
echo "</a>";
echo "</div>";
echo "</div>";
}
?>
Current output
Expected output
Can anyone suggest me how should i get this output.
Check below snippet,
$inc = 4;
$i = 1;
while ($row = mysqli_fetch_array($result)) {
if (empty($temp) || $inc != $temp) {
$temp = $inc;
if ($inc == 4) {
echo "<div class='gallery-grid'>";
}
echo "<div class='gallery-items'>";
}
if ($i <= $inc) {
// echo $i . '<>';
echo "<div class='mansory-item'>";
echo "<a href='admin/images/" . $row['username'] . "' data-lightbox='gallery' class='ansa-thumb'>";
echo "<img src='admin/images/" . $row['username'] . "' class='item-img img-1'>";
echo "</a>";
echo "</div>";
$i++;
}
if ($i == $inc) {
echo "</div>";
if ($i == 3) {
echo "</div>";
}
$i = 1;
$inc = ($inc == 4 ? 3 : 4);
}
}

Dynamic Table Continuous <td> Count

I am stuck in a weird problem, I want to show the continuous number till the last in the row and cols,
instead of that its showing form 1-50 and then again the row starts with 1 up-to 50.
<?php
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>$td</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Thanks
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
$style = "green";
if ($tr % 2 == 0) {
$style = "#ccc";
}
echo "<tr style='background-color:".$style."'>";
for($td=1;$td<=$cols;$td++)
{
echo "<td>$x</td>";
$x++;
}
echo "</tr>";
}
echo "</table>";
You are outputting $td which resets at every new tablerow.
Instead, you would like to output an incrementing $x, if I'm not mistaken.
<?php
$rows = 10;
$cols = 50;
$x=1 ;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>" . $x . "</td>";
$x++;
}
echo "</tr>";
}
echo "</table>";
?>
In response to your comment on another answer: if you would want alternating row colors, you could use $tr and check wether it is even or uneven:
if($tr % 2 == 0)
{
// use color1
}
else
{
// use color2
}

mysqli/PHP - first two rows never show up(table with row data going across columns then moving down)

My website currently ignores the first two images you place into the database and then proceeds to add images going across 5 columns and then moving down to the next row.
Update: Now it shows 3 of the 4 images in the database. Skips one image.
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
This is what my database looks like
http://i.stack.imgur.com/IFba8.jpg
This is what my website shows
http://i.stack.imgur.com/Wf7E1.jpg
Try this:
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
Please try this.
<?php
$i = 1;
echo "<table>";
while ( $row = $Recordset2->fetch_object() ) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>

PHP Fatal error: Cannot access empty property in a script I'm trying to install

I'm totally new at this and have run into a small glitch on a new install and I'm not sure how to fix it.. :(
The error is apparently on this line of code:
$r2->$k = htmlspecialchars($r2->$k);
Here is the code above and below:
// Display items
while ($r2 = mysql_fetch_object($qr2)) {
reset($_bnr_list);
echo "<tr>\n";
$i = 0;
while (list($k, $v) = each($_bnr_list)) {
$i++;
$r2->$k = htmlspecialchars($r2->$k);
if ($kq)
$r2->$k = preg_replace($ks, "<b>\\0</b>", $r2->$k);
echo "<td class=row2>";
if ($i == 1)
echo "Edit: <a href=admin.php?a=bnr/edit&id={$r2->id}&{$_fwk_id}>", ($r2->$k != "" ? $r2->$k : "no name"), "</a>";
elseif ($i == 2) {
$mod = "usr";
list($un) = mysql_fetch_row(mysql_query("SELECT username FROM " . $GLOBALS["_{$mod}_tables"]['list'][0] . " WHERE id={$r2->$k}"));
echo "<a href=admin.php?a={$mod}/info&id={$r2->$k}&{$_fwk_id}>{$un}</a>";
} elseif ($i == 5)
echo round($r2->stat_clicks * 100 / $r2->stat_shows, 3), "%";
else
echo ($r2->$k != "" ? $r2->$k : " ");
echo "\n";
}
echo "<td class=row2><small>",
"<!--- <a href=admin.php?a=bnr/del&id={$r2->id}&{$_fwk_id} {$_fwk_js_confirm}>Delete</a> --->",
"</small>";
echo "<tr><td colspan=", ($col + 1), ">", $r2->html;
}
That error means $k is empty and can not be used as property name. $k is the key from $_bnr_list so you have to trace where does it come from and why does it have empty strings / null values as keys.

Adding numbered rows/serial numbers to database query result set

Hello i'm having problems adding numbered rows/serial numbers to my database query result. I've used $number to collect the actual number of rows.
Then another problem i'm trying to avoid is: Numbering of Column Headers.
Thanks for the help.
<?php
$number = mysql_num_rows($query);
for ($serial = 0; $serial < $number; $serial++)
{
echo "<tr>". $serial ."</tr>";
}
for ($i = 0; $i < $number_cols; $i++)
{
echo "<th>" . mysql_field_name($query, $i) . "</th>\n";
}
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
for ($i = 0; $i < $number_cols; $i++)
{
echo "<td>";
if (!isset($row[$i]))
{
echo "NULL";
}
else
{
echo $row[$i];
}
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>";
echo "</span>";
echo "</div>";
?>
trying my best to get something sane out of your terrific code.
<?php
$serial = 1;
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
echo "<td>";
echo $serial++;
echo "</td>\n";
foreach ($row as $value)
{
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}

Categories