I have a table in my database with 100 names, dates, id's, and other stuff. I want to be able to get a specific amount of rows echoed in a div, and then another specific amount in the next and so on.
I have been looking at foreach and break here, cause my googling kind of sent me there.. but maybe im looking at the wrong thing:
http://php.net/manual/en/control-structures.break.php
I just don't seem to get things right.
This is a school project and I know this is maybe a little bit vague, sorry about that.
Something like this:
<div id="div1"> echo row 1-7 </div>
<div id="div2"> echo row 7-19 </div>
<div id="div3"> echo row 20-44 </div>
and so on...
Could someone point me in the right direction?
You can do it directly in your query
Use at the end of your query this:
<div id="div1"> echo row 1-7 </div> use "LIMIT 0,6"
<div id="div2"> echo row 7-19 </div> use "LIMIT 7, 19"
<div id="div3"> echo row 20-44 </div> use "LIMIT 20,44"
if you have a numerically indexed array holding your rows you could do something like:
<div id="div1">
<?php for ($i = 1; $i <= 7; $i++) {
print $yourArray[Si]['some_field'];
} ?>
</div>
<div id="div2">
<?php for ($i = 8; $i <= 19; $i++) {
print $yourArray[Si]['some_field'];
} ?>
</div>
...
Something like this (not tested) might work:
$id=1;
$count=0;
$limit=20;
foreach($row as $r){
if ($count==0) echo "<div id='div$id'>";
echo $r;
if ($count++==$limit){
$count=0;
$id++;
echo "</div>\n";
};
}
if ($count!=0) echo "</div>";
Related
This question already has answers here:
Echo current number of row
(3 answers)
Closed 1 year ago.
I have a query that I make using PHP and it returns me all the rows that satisfied the condition that was given, now, I'm wondering if there's a way to count each number of row a result of a query has and pass that number to an id of an element on HTML?
<?php
$res2 = mysqli_query($con,"SELECT NAME, PRICE FROM product WHERE AVAILABLE = 1 AND ID_CATEGORY = 17");
?>
<body>
<?php
while($row = mysqli_fetch_row($res2)) {
print_r($row);
?>
<div class="item">
<img class="trj_img_cls" src="RSC/COMING_SOON.jpg" alt="" id="imgDG<?php ?>">
<div class="texto_trj">
<div class="nombre_producto">
<h2 id="ttlDG<?php ?>">tÃtulo</h2>
</div>
<div class="precio_producto">
<i class="icon-dollar"></i>
<label id="prDG<?php ?>">0.00</label>
</div>
<div class="caracteristicas">
<ul id="caractDG<?php ?>"></ul>
</div>
</div>
</div>
<?php
}
?>
</body>
I put <?php ?> because there I want to add the variable (number of the row) so it complements the actual id and then i'm able to look for that id with javascript. Another thing is that I use the print_r() just to see if the elements were appearing.
This can be done by setting an $increment variable to 1 at the start of the while loop, and incrementing it at the end of the loop.
<?php
$increment = 1;
while($row = mysqli_fetch_row($res2)) {
And then using it like this:
id="imgDG<?php echo $increment; ?>">
And then incrementing it at the end:
<?php
$increment++;
}
?>
For parsing a list of articles, i have this code to parse all the articles:
while($article = $articles->fetch())
{
$date = strtotime($article['createdAt']);
$formatted_date = date("F Y",$date);
?>
<br />
<div class="news-content">
<div class="news-image">
<?php echo $article['title']; ?>
</div>
<div class="news-article">
<h3>
<span><?php $date = strtotime($article['createdAt']); echo /*date("F j",$date);*/ strftime('%e %B',$date) ?></span>
<br />
<?php echo $article['title']; ?>
</h3>
</div>
</div>
<?php
} //end while loop
?>
What i want to achieve: only the first 5 <div class="news-content">...</div> should be shown.
I know i have to do something with a for loop
but i do not know exactly how to use the for loop for this situation...
Can someone help me with that?
There are a lot of different ways to limit a loop. One possibility is to use a for loop instead of a while loop. for is often a good option if you want something to happen a specific number of times. Adding something else like fetch into the continuation condition will mean it happens up to a specific number of times.
for ($i = 0; $i < 5 && $article = $articles->fetch(); $i++) {
// output article
}
I want to fetch some data from database in the form of list. Following is my loop:
<div class="col-sm-4">
<ul>
<?php
foreach($subjects as $s)
{
echo "<li>$s->subject_title</li>";
}
?>
</u>
</div>
Now this loop prints data in one column and the user have to scroll down to view the last elements. I want to control the loop in such a way that after printing first 10 elements, it must start again from the div tag and print the next 10 elements and so on. I want to show the data in such a way that the user don't have to scroll down.
For example, I want to print the data in the following way:
I write the following code but it is not working. The div tag is only applied to the first 10 elements, and not on the rest of the data.
<div class="col-sm-6">
<ul class="filter-list">
<?php
$i = 0;
foreach($main_subjs as $i=>$ms)
{
if($i == 10) {
echo "</ul></div><div class='col-sm-6'><ul>";
$i++;
continue;
}
echo "<li><a href='#'>$ms->mains_title</a></li>";
}
?>
</ul>
</div>
Please Help.
As an arugment for a loop you can also use the key of array
foreach($subjects as $i=>$s)
When you will be able to "control" your loop, e.g.:
<div>
<?php
foreach($subjects as $i=>$s) {
if($i> 0 && $i % 5 == 0) {
echo "</div><div>";
}
echo "<li>$s->subject_title</li>";
}
?>
</div>
I have a series of repeatable div's where the user can go in and add as many as they want, I just want to number the div's. The idea I have now is to use PHP to count them and echo the number from 1 to infinity with 1 being the first div and so on.
I'm not sure how this works, but I think we have to start with some math?
<?php $i = 0; ?>
...and then something about foreach div.tiles-wrap echo $i + 1...?
<div class="tiles-horizontal">
<div class="tiles-wrap">
<div class="title"><p class="center"><?php echo i; ?></p></div>
<div class="image"><img src="#" /></div>
</div>
</div>
Thanks in advance for any help!
After display count number in each div you need to increment $i value, like this:
<div class="tiles-horizontal">
<div class="tiles-wrap">
<div class="title"><p class="center"><?php echo $i; ++$i;?></p></div>
<div class="image"><img src="#" /></div>
</div>
</div>
Put:
<?php $i++; ?>
at the bottom of the loop body to increment the counter variable.
This really is not any kind of math problem just very simple programming. If you wish to accomplish this with php you will need something like
Lets say you have a button they press each time they wish to add another div in php we take care of it like this:
Forgot to add we need a Session variable to keep track of it
if(isset($_POST['submitbutton']))
$_SESSION['count']++;
...
After you only need to do a for loop and echo out as many div's as you need like this
<?php for($i=0; $i<$_SESSION['count'];$i++): ?>
<div>
<p> Div Number <?php echo $i ?> </p>
</div>
<?php endfor ?>
And thats it you may also need to add a button like this :
<form action="" method="POST">
<input type="submit" name="submitbutton" value="Click to add div" />
</form>
Yes, I've looked around and yes I've used mysql_fetch_array hundreds of times successfully before. I want a php file that will output an html file with some constants and a few interpreted php variables. After an hour, this is where I'm at.
<?php
require "(this is sql statement to get all rows from the db).php";
$i = 1;
while($row = mysqli_fetch_array($results_getpieces) && $i < 11 ){ ?>
<li>
<a href="#image-1">
<img src="<?php echo $row['content']; ?>" alt="image01">
<span><?php echo $row['title']; ?></span>
</a>
<div class="lb-overlay" id="image-1">
<img src="<?php echo $row['thumb']; ?>" alt="image01" />
<div>
<h3>blahblah <span>bluelue</h3>
<p>sherpas don't dance</p>
Prev
Next
</div>
x Close
</div>
</li>
<?php
$i++;
}
?>
When I display the $row results in a table, there's no problem. Before I added that i++ business, I could get the first record from my results, but it would just repeat those results for all ten instances.
Help almighty stackers, and overflow me with your knowledge. Please. I can't go to sleep until I get this. Where am I going wrong?
You need to parenthesize the condition appropriately, so that the array that you get back from the function call doesn't get "and"-ed with the condition "$i < 11":
($row = mysqli_fetch_array($results_getpieces)) && $i < 11