Please Do see the code below . . I have restricted the results to 3 and i have added a more link at the end but it isnt working ! Can you please check this out . . . I have tried multiple ways
<?php
if(count($this->store_list)>0){
$number_output = 0;
foreach($this->store_list as $store){
$number_output++;
if($number_output > 3) break;
?>
<div id="menu">
<span class="cons_title fl"><h2><?php echo ucfirst($store->name); ?></h2></span>
<div class="cons_bot fl clr">
<ul>
<li><h4><?php echo $store->address1; ?>,</h4></li>
<li><h4><?php echo $store->address2; ?>,</h4></li>
<li><h4><?php echo $store->area_name; ?>,</h4></li>
<li><h4><?php echo ucfirst($store->city_name); ?>,</h4></li>
<li><h4><?php echo $store->pin; ?></h4></li>
<li><h4>PH:</h4><h4><?php echo $store->phone; ?></h4></li>
</ul>
</div>
</div>
echo "$newtext <a href=more_details.php> more...</a>
you need to indicate to your code where to start from on the next page for example, you could use the following link: href="more_details.php?position=3" and that page will have the link: href="more_details.php?position=6" and so on.
In your code you would use the value of position to figure our from what offset to display the next set of results.
Can you give us details on what isn't working? If you are talking about having the more details page not working, then you need to post that.
Right away though I see that you forgot to finish your last link and finish your if/foreach statement, unless you arent showing us all the code.
Its also confusing why you have a a header tag in your list tags.
on the first load of the page, you set a session variable on which you place the last row you loaded (let's say 30) and the rows you are about to display (rows 1-30).
when you click "more...", you need to know in the PHP side that it was the "more..." button. if it's "more..", grab the last row number stored in the session and add 1. that will be your starting point for another 30 items. get the items 31-60, append it to the previously stored rows (the ones in the session). then render them. now you have 60 items displayed, 60 as your last item count and 60 in store. repeat the procedure for more.
so you ask why you need to determine if it was the "more..." button. if you didn't, every load to the page would load the previously stored items plus the next 30. so if it isn't the "more..." button, clear the session and start a new 30 items.
Related
Below is my Code.
<?php
$folder_name=$_GET['folder_name'];
$dirname = "customized_kits/images/";
$images = glob($dirname."*");
$filecount = count( $images );
$i=0;
for($i=0;$i<200;$i++) { ?>
<div class="col-md-2"> <div class="photos">
<div id="images1">
<a href=# data-lightbox="roadtrip">
<?php
if($filecount>$i) {
$filename = substr($images[$i], strrpos($images[$i], '/') + 1);
echo '<img src="'.$images[$i].'" style="width:150px;height:150px" /><br/>';
?>
<i class="fa fa-remove"></i>
<?php } else {
echo '<a ><img style="width:150px;height:150px;background-color:#eee" /></a>';
} ?>
</div>
</div>
</div>
<?php } ?>
Here I created 200 boxes with foreach loop I want to show 20 divs for single page and with help of pagination I want to show other divs.I searched in many websites I didnt get exact answer please help me to get out of this issue.
Okay so from your code you can have 200 values of $i and you want to paginate them in segments of 20. Let us start off by adding a script above your code to get the range that needs to be displayed. Page number comes as a GET parameter in number. ex example.com/feeds?number=2.
<?php
$pageno = $_GET['number']; // Page number from frontend
// Now this value will give you 1,2,3 etc
$start = $pageno*20;
// $start contains the initial offset or the value of `$i` from to start
for($i=$start;$i<$start+20;$i++) {
// Will only show 20 elements on the page
}
?>
Remember this is a very basic example. Although i would recommend using range in sql query instead so you don't have to load the complete dataset in each query.
More explanation
Suppose you have M results and you want to show N results on each page, the results on page x would start from x*N to x*(N+1) pure math here. In your code you are iterating $i over the whole loop rather just iterate over that specific range to get results. The value of x is the example is the page number that we get from number in GET verb.
I'll try to be clear. My problem is that i'm using php to get data from mysql. The table has more than one record. To show table records i'm using a while loop that as a condition has mysqli_fetch_array(). The records from the table must be shown in an echo(because i want to show them as html on the page), but every div generated from the while loop has a link to send me to another page, and if the client clicks on that link it will get the current divs information(after the table in database has more than one record the informations will be different) to be shown at the next page. Hope you understood it. Thanks in advance!
while($rows1 = mysqli_fetch_array($query1,MYSQLI_ASSOC))
{
echo "<div class=\"row\">
<div class=\"col-md-6\">
<div class=\"thumb\">
<figure>
<img src=\"images/extra-images/room-grid1.jpg\" alt=\"\"/>
<figcaption>
<a rel=\"prettyPhoto[gallery2]\" href=\"images/extra-images/room-grid1.jpg\">
<i class=\"fa fa-search\"></i>
</a>
</figcaption>
</figure>
</div>
</div>
<div class=\"col-md-6\">
<div class=\"text\">
<h4>".$rows1['Dh_lloji']."</h4>
<p>".$rows1['Dh_Pershkrimi']."</p>
<ul class=\"room-grid-meta\">
<li>Max: ".$rows1['Dh_Kapaciteti']."</li>
<li>Size: ".$rows1['Dh_madhesia']."</li>
<li>Floor: ".$rows1['Dh_Kati']."</li>
</ul>
<div class=\"retail room-grid-retail\">
<span>
<sup>$</sup>
".$rows1['Dh_cmimi']."
<sub>night</sub>
</span>";
if(isset($_SESSION['Emri_Mbiemri']) != '')
{
echo "<a class=\"btn-3\" href=\"payment.php\">Book now</a>";
}else{
$error = "You must be logged in to book!";
}
echo "<br>".$error."</div>
<div id=\"price_room\">".$rows1['Dh_cmimi']."</div>
</div>
</div>
</div>";
$count++;
}
Looks like you need at least your custom counter, that will be increment inside cycle, and set div's id based on this counter plus use it in generated link
I made a solution for my problem and i wanted to share it with you guys. I gave a $count to the links defining every link the while loop generates...
<a class=\"btn-3\" href=\"payment.php?id=$count\">Book now</a>
After that i used a multidimensional array to store the current divs variable(in my case rooms price, the count, and the id) of the room from database...
$room_prices[$count][0] = $rows1['Dh_cmimi'];
$room_prices[$count][1] = $count;
$room_prices[$count][2] = $rows1['ID_Dhoma'];
After the client click a specific link he will get the id so i can make a search at my multidimensional array for the price and the id of that room..
for($i = 0; $i < $count; $i++)
{
if($_GET['id'] == $room_prices[$i][1]){
$sess_room_price = $room_prices[$i][0];
$sess_room_id = $room_prices[$i][2];
}
}
After i get these i store them at the global session variables to use them at the other page...
$_SESSION['Rooms_Price'] = $sess_room_price;
$_SESSION['Rooms_ID'] = $sess_room_id;
So this did the work for me... Hope this helps others.
i have these lines of code:
<div class="signals">
<ul>
<li>First Signal</li>
<li>Second Signal</li>
<li>Third Signal</li>
</ul>
</div>
<div id="signal1" style="display:none;">
<p style="color:#fff">First comment for #signal1 id - it will open in a fancybox -.</p>
<div id="signal2" style="display:none;">
<p style="color:#fff">Second comment for #signal2 id - it will open in a fancybox -.</p>
</div>
<div id="signal3" style="display:none;">
<p style="color:#fff">Third comment for #signal3 id - it will open in a fancybox -.</p>
</div>
Here it is the jsfiddle code: JsFiddle
Right now when i want to show different comments, i open my html file and edit the "id #signal , adding more id (or deleting them) when i need it.
The problems is: the signals can be more than the three that are showed up, or even less.
So my question is:
There is a way to generate automatically the divs that i need in a second sheet, where i will insert the comment and all the id's? (a sort of backend)
For example: if one day i need just 2 signals, i will create the #signal1 and #signal2 div, i'll insert the comments and save the secondary sheet.
When i do that, the primary sheet with the html stuff, will show 2 "li" lines:
First Signal
Second Signal
and when i click on of them, the fancybox will open and show the comment, just as the code into the jsfiddle.
I don't know much of php, but there is a way i can do that using it? Or there is a better way?
Hope i can learn from your help.
It seems that you need PHP foreach loop.
You can write the comments in a PHP array, and read them using PHP foreach.
Here is the sample code:
<?php
$commentsLabels = array('First Signal','Second Signal');
$commentsTexts = array('First comment for #signal1 id - it will open in a fancybox.','Second comment for #signal2 id - it will open in a fancybox.');
//You could use a 2D array to store both comments Labels and comments Texts in one array, but since you are new to PHP, I put them in 2 different arrays to make it easy.
//You can add more comments texts/labels to the arrays in double/single quotes separated with ','
//For now, the arrays are filled manually. in future, you can add a DB to store comments and fetch the values from there.
$counter = 0;
//we need a counter to make the comment ids dynamic
?>
<!--GENERATING LABELS-->
<div class="signals">
<ul>
<?php
foreach ($commentsLabels as $commentLabel) { //loop starts
$counter++; //increasing $counter by 1, for each iteration
//We have to add HTML tags outside the php code block.
?>
<li><?php echo $commentLabel; ?></li>
<?php
} //loop ends
?>
</ul>
</div>
<!--GENERATING POPUPS-->
<?php
$counter = 0; //reset counter
foreach ($commentsTexts as $commentText) { //loop starts
$counter++;
?>
<div id="signal<?php echo $counter; ?>" style="display:none;"><p style="color:#fff"><?php echo $commentText; ?></p></div>
<?php
} //loop ends
?>
I hope this helps you start. :-)
I have my data being output to a span currently... this is how it looks:
Now, when i remove the span and place a div there i am given this output:
This is desired, but I want to set a height to my page and have the data show up in as little as 3 columns. How would I do this? I have searched everywhere online but can't seem to find anything that shows a solution.
I did read that some use javascript for the format but i am still clueless on even this option.
My desired output would look like this:
If you know how many items you want in a column then you can seperate them out into individual divs and then float those divs to the left to get them to be next to each other.
<div style='float:left'>
//Items go here
</div>
<div style='float:left'>
//Items go here
</div>
etc.
If you figure out how many items your query returned, say using mysql_num_rows() and divide by 3 you can tell how many to put in each column.
Also be sure to clear the floats afterwards, so like this:
<div style="clear:both"></div>
Sometimes this is necessary as there will be random issues if this is not put there.
What you are describing can be solved with styling only. You have several divs that must be displayed in columns. The easiest way is floating them to the left, and setting the width for 1/3 of the parent. If you want 4 columns, set the with to 1/4 of the parent, and so on.
<div class='sqlResult' style="float:left;width:33%;">
<a href='#'>$key</a>
</div>
Also as other answers mentioned, don't use duplicated ids. Always use classes. If you need to target each div individually, give it a unique id, such as "category_1", "category_2", and so on.
This should work
<table><tr>
<?php $count=0; $total=mysqli_stmt_num_rows($sql)-1; $idxcount=0; $limit=10; while($row = mysqli_fetch_array($sql)): $key = $row['Keyword_Name']; ?>
<?php if($count == 0){ echo '<td>';} ?>
<span>
<?php echo $key; ?>
</span>
<?php if($total == $idxcount): ?>
</td>
<?php elseif($count == $limit): ?>
</td>
<?php $count=0; else: $count++; ?>
<?php endif; $idxcount++; ?>
<?php endwhile; ?>
</tr></table>
This is probably a stupid gotcha that I'm overlooking but I'm hoping one of you can help me!
I've got a loop to list a grid of Products in my DB.
So far so good, everything is displaying roughly OK except this one little issue.
Within a list I'm doing the following:
<ul>
<?php $i=0; foreach ($products as $product) : $i++; ?>
<li <?php
if(($i%4) ==0){
echo 'class="last"';
} elseif($i%2==0){
echo 'class="second"';
}
?>>
// Then I've got the image thumbnail etc coming in....
All looks good except for the LAST row...
So for instance if I have 8 products... the first 7 will display on the page correctly, but then there is a gap at the end where the 8th product moves onto the next page.
At first I thought it was CSS widths or something but it's not. Even if I have 20 products...always the last row only shows 3 across and puts the last product on the next page.
Any ideas anyone?
Cheers M
<ul>
<?php $i=count($products); ?>
<li
<?php
if(($i%4) ==0){
echo 'class="last"';
} elseif($i%2==0){
echo 'class="second"';
}
?>>
Let's try it..