I'm switching a site that used to use mysqli to PDO. I have page that generates a <li> menu, split into two columns (ie two container divs).
In the mysqli version, I have the following code - $num is the number of rows returned by my query, $recsToOutput is half that number:
<div class="6u">
<ul>
<?php //Loop through first half of table and output name and link...
for($i=1;$i <= $recsToOutput; $i++){ // for the first record to the half the number of rows...
echo '<li><a href="#">';
echo '<h3>'.$row['menu_item'].'</h3>';
echo '</a></li>';
$row=mysqli_fetch_array($r,MYSQLI_ASSOC);
}
?>
</ul>
</div>
<div class="6u">
<ul>
<?php //.. loop through remaining entries and output
for($i=$recsToOutput; $i < $num; $i++){ // for half the number of rows to the full number of rows...
echo '<li><a href="#">';
echo '<h3>'.$row['menu_item'].'</h3>';
echo '</a></li>';
$row=mysqli_fetch_array($r,MYSQLI_ASSOC);
}
?>
</ul>
</div>
Here, the mysqli_fetch_array calls moved the array pointer of $row on one in order to grab the next menu item. However, I obviously can't use that with PDO: how can I replicate the functionality (or do it better?)
Related
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 currently have a simple html list. What I want is to repeat this list item with each repeated element having a variable that will increase by one, each time it is repeated.
The php calls that i have included in the above list item is from the Advanced Custom Field plugin on WordPress. The variable I want to change is the number inside of those php calls.
<ul>
<li>
<?php the_field('vendor_1_name'); ?> <!--the number is the variable that i want to increase by one with each repition-->
<a href="http://<?php the_field('vendor_1_url'); ?>" target="_blank"/>
<?php the_field('vendor_1_url'); ?>
</a>
</li>
</ul>
I have an idea of how to do this using an array, but I would love to hear other methods on how to do something like this. Thanks!
<ul>
<?php for ($z = 0; $z < $n; $z++) : ?>
<li>
<?php the_field('vendor_'.$z.'_name'); ?>
<a href="http://<?php the_field('vendor_'.$z.'_url'); ?>" target="_blank">
<?php the_field('vendor_'.$z.'_url'); ?>
</a>
</li>
<?php endfor; ?>
</ul>
Where $n is the number of times it should iterate.
Use a for loop. If you have an array of items (are you grabbing vendors from a database?), just iterate through the array:
<?php
// fetch values and define $vendors
// using sql is highly recommended
echo "<ul>\n";
for ($i = 0; $i < count($vendors); $i++) {
echo "<li><a href='".$vendors[$i]['vendor_url']."'>".$vendors[$i]['vendor_name']."</a></li>\n";
}
echo "</ul>\n";
?>
Note that the array would say vendor_name instead of vendor_1_name because the field is not going to have the iteration in its name.
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>";
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
I have this PHP function, which I use to display my userdata. If run once it will basicly display a div with the userdata in it.
function listings($fornavn, $efternavn, $email, $adresse, $tlf, $postnr, $city, $fodselsdag, $brugerid,$bartype,$idbar)
{
?>
<div class="container">
<span class="records" id="id-<?php echo $brugerid;?>">
<div class="customer bidDiv clearfix">
<?php if ($bartype=='temp_vip') { ?>
<ul>
<li>
<span class="actionSpan" id="<?php echo $brugerid;?>" value="<?php echo $idbar;?>">
<a class="edit-opt" id="godkend" href="#">GODKEND</a>
</span>
</li>
<li>
<span class="actionSpan" id="delete-<?php echo $brugerid;?>" value="<?php echo $bartype;?>">
<a class="delete-opt" id="delete" href="#">Afvis</a>
</span>
</li>
<?php }else{ ?>
<ul>
<li>
<span class="actionSpan" id="delete-<?php echo $brugerid;?>" value="<?php echo $bartype;?>">
<a class="delete-opt" id="delete" href="#">Slet</a>
</span>
</li>
<li>
<a class="edit-opt" href="editform.php?id=<?php echo $brugerid."&bartype=".$bartype;?>" rel="facebox">Rediger</a>
</li>
<?php if ($bartype =='vip'){?>
<li>
<a class="print-opt" href="print.php?id=<?php echo $brugerid;?>" rel="facebox">Print</a>
</li>
<?php }else{
// Dont render vip link
}}?>
</ul>
<p class="contact-data">
<?php echo $email;?><br>
Tlf.: <?php echo $tlf;?>
</p>
<div class="base-data">
<h4><?php echo ucwords($fornavn)." ".ucwords($efternavn);?></h4>
<p>Fødselsdag <?php echo $fodselsdag;?></p>
<address><?php echo ucwords($adresse) ." ". $postnr ." ". ucwords($city);?></address>
</div>
</div><!-- end customer -->
</div>
</span>
<?php
I mostly use this function in a loop, to display all users from my MySQL database, who has a specific relation to something else.
This is all working great. But currently it would pull all results at once. Right now I only have 5 members, so its no problem, but say I get 800 or 2000 members, the list of users would get very long.
So I want to implement some kind of limit on how many users it displays, but still be able to browse through all the users. Many sites use something like pages and split up the results that way. Links like:
[1] [2] [3] [Last page>>]
I can't figure out how to start doing this? How would I proceed?
Also looked into this Jquery plugin:
http://andersonferminiano.com/jqueryscrollpagination/
But it keeps reloading my PHP file which results in the Loops being restarted, and it simply displays the results over and over again endlessly.
Can someone help me with the logic behind creating this? Or better, point me in a direction where I could use the jquery pluging above - where it only loads the loop one time, and renders the results as I scroll.
Any help or pointers will be greatly appreciated!
Thanks
Jquery pagination by default will only break-up the given table into several tabs on the page, that is all the data is actually loaded. There are some that let you do an AJAX request to fetch the next page, to work in conjunction with your PHP code.
Ideally, you want to limit the query using LIMIT (number of rows in the result object) and OFFSET (Start from row X ) which will give you number of records starting from your offset row up until the limit, and then use logic on the PHP side to determine the maximum number of pages.
$page = $_POST["page"] * 25; // make sure that page 0 is the starting page or calculate
$SQL = "SomeQueryText LIMIT 25 OFFSET '$page'" ;
$result = query ($SQL);