I have a dynamically generated DIV
<div class="content drag-desired">
<?php
$result = mysql_query("SELECT * FROM XXXX WHERE qty != 0");
while($row=mysql_fetch_assoc($result))
{
echo '<div class="product"><img src="img/products/'.$row['img'].'" alt="'.htmlspecialchars($row['name']).'" width="128" height="128" class="pngfix" />
<div>'.$row['price'].'$</div></div>';
}
?>
<div class="clear"></div>
</div>
the while loop makes the list be very long,
Any idea how to make the div contains 6 items only and show the fetched items 6 by 6?
I don't know the logic behind the scene. :)
I will be appreciated, if someone explain the follow chart for making the div slides.
Thanks
Like so:
<div class="content drag-desired">
<?php
$result = mysql_query("SELECT * FROM XXXX WHERE qty != 0");
$counter = 0;
while($row=mysql_fetch_assoc($result))
{
if($counter==0)
echo '<div class="slide">';
echo '<div class="product"><img src="img/products/'.$row['img'].'" alt="'.htmlspecialchars($row['name']).'" width="128" height="128" class="pngfix" />
<div>'.$row['price'].'$</div></div>';
if($counter==5)
echo '<div>';
$counter++;
if($counter > 5)
$counter = 0;
}
?>
<div class="clear"></div>
</div>
So, the code below counts to 6 and wraps 6 items inside "slide". I think you understand the logic I used :) And with a little hint of CSS/JavaScript you can make your own slider that changes slide to be shown.
Related
Currently when using the below code, I get website that looks like this:
Image here (Had to paint out some info due to GDPR)
This is the code used:
<div class="page group">
<div class="section">
<div class="col span_1_of_3">
<div id="outer_wrapper">
<div id="inner_wrapper">
<?php
$query = "SELECT * FROM sales WHERE ident ='".$currentName."' AND status ='Venter' ORDER BY STR_TO_DATE(date, '%d.%m.%Y') DESC";
if ($result = $mysqli->query($query)) {
$num_rows = 0;
while ($row = $result->fetch_assoc()) {
$num_rows++;
if ($num_rows > 0) {
echo '<div class="box">';
echo '<div class="Nwrapper">';
echo '<div id="NformContent">';
echo
"<tr><br>
<td><b>{$row['date']}</b></td><br>
<td> {$row['name']} </td> <b>/</b>
<td>{$row['gsm']} </td><br>
<td> {$row['email']} </td><br><br>
<td> <b>Info</b> <br> {$row['pp']} </td><br>
<td>Portering: {$row['transfer']}</td><br>
<td><a href='delete.php?id={$row['id']};' class='aRS'>Oppdater status</a><a onClick=\"javascript: return confirm('Ønsker du å angre salget?');\" href='delete.php?id={$row['id']};' class='aR'>Slett salg</a></td><br><br>
</tr>";
echo '</div>';
echo '</div>';
echo '</div>';
} else {
echo "No appointments";
break;
}
}
/*freeresultset*/
$result->free();
}
?>
</div> <!-- inner_wrapper -->
</div> <!-- outer_wrapper -->
</div> <!-- col span_1_of_3 -->
</div> <!-- section -->
</div> <!-- page group -->
<div class="lineWrapper"></div><br>
What I want to do is to hide the top horizontal boxes if the query return no value(0 number of rows(?) num_rows). I have tried doing it like this, and put my whole code inside the PHP query:
<?php
$query = "SELECT * FROM sales WHERE ident ='".$currentName."' AND status ='Venter' ORDER BY STR_TO_DATE(date, '%d.%m.%Y') DESC";
if ($result = $mysqli->query($query)) {
$num_rows = 0;
while ($row = $result->fetch_assoc()) {
$num_rows++;
if ($num_rows > 0) {
?>
<div class="page group">
<div class="section">
<div class="col span_1_of_3">
<div id="outer_wrapper">
<div id="inner_wrapper">
<?php
echo '<div class="box">';
echo '<div class="Nwrapper">';
echo '<div id="NformContent">';
echo
"<tr><br>
<td><b>{$row['date']}</b></td><br>
<td> {$row['name']} </td> <b>/</b>
<td>{$row['gsm']} </td><br>
<td> {$row['email']} </td><br><br>
<td> <b>Info</b> <br> {$row['pp']} </td><br>
<td>Portering: {$row['transfer']}</td><br>
<td><a href='delete.php?id={$row['id']};' class='aRS'>Oppdater status</a><a onClick=\"javascript: return confirm('Ønsker du å angre salget?');\" href='delete.php?id={$row['id']};' class='aR'>Slett salg</a></td><br><br>
</tr>";
echo '</div>';
echo '</div>';
echo '</div>';
?>
</div> <!-- inner_wrapper -->
</div> <!-- outer_wrapper -->
</div> <!-- col span_1_of_3 -->
</div> <!-- section -->
</div> <!-- page group -->
<div class="lineWrapper"></div><br>
<?php
} else {
break;
}
}
/*freeresultset*/
$result->free();
}
?>
When I save, it all looks good, the top boxes are all gone, but now if the number of rows is higher then 0, and there are some value to show, it looks like this(each displays in a vertical order):
Image here
Any suggestions on how I can get this to work like I want it to?
There is while loop. Inside loop you open table row put chunk as $num_rows and close row, than goes another chunk in it's own row.
While loop works as loop - chunk after chunk - end of loop;
I'm not sure - you want them all horizontally? In one row?
Actually, you don't need any table for this, this isn't tabular content, more appropriate would be section or div or something similar.
Whatever you pick, for now it would be a 'box';
this is how it should go:
open a box,
query, fetch - if is empty do nothing {}
if isn't empty - write down chunk after chunk,
close box;
With no chunks it will be empty box - you can hide it with .css (or not...)
Other solution is to assign all chunks to variable $x.= chunk
and then - if (x) is not empty - open a box - write it (x) down - close box;
If the output (x) is big, may cause problem.
Next one solution - workaround :
$box = '<div>'; // echoing $box will open a box
query, fetch, if empty do nothing;
if not empty output - echo $box write first chunk, unset $box - following chunks won't echoing $box,
after loop, if $num_rows > 0 close box (if 0 rows there is no open box)
I have these cards with data that comes from a database. This is the reason it is in a while loop.
Now my problem is that I don't have a clue how to add pagination to this. If some of you guys have any suggestions or answers please let me know.
$start_from = ($page - 1)*$record_per_page;
$query = "SELECT organisation_name, organisation_logo, organisation_url FROM `organisations`";
$result = mysqli_query($conn, $query);
if ($result ->num_rows > 0) {
echo '<div id="myItems">';
echo '<div class="row">';
while ($row = $result-> fetch_assoc()) {
echo '
<div class="col-md-6">
<div class="card rounded-lg mt-3">
<div class="card-body p-0 shadow">
<div class="row">
<div class="col-md-4 p-0">
<img src="../img/';
if ($row['organisation_logo'] == '') {
echo 'stock/no-image-icon.png"';
} else {
echo 'uploads/'.$row['organisation_logo'].'"';
}
echo 'class="border p-3" style=" width: 150px; height: 150px;">
</div>
<div class="col-md-8">
<h3 class="card-title mt-3">'.$row["organisation_name"].' <a class="text-dark" href="http://'.$row["organisation_url"].'" target="_blank"><i class="fas fa-external-link-alt" style="font-size: 18px;"></i></a></h3>
<p>Current active surveys: <b>0</b></p>
<b>View <i class="fas fa-arrow-circle-right"></i></b>
</div>
</div>
</div>
</div>
</div>';
}
echo '</div>';
}
else {
echo 'nothing here';
}
You need to use LIMIT and OFFSET functionality in your SQL query. Limit determines how many elements you want to show on one site. Offset is point from where you want to get your items. So, for example – if You want 5 items, and you are on page 3, you have to add LIMIT 5 OFFSET 10. You calculate offset with (page_number-1) * limit equation. If you want to count how many pages you have, you must get total pages number, and use something like $pages = ceil($total / $limit). There are a lot of examples on stackoverflow.
I think this one can be very helpful – Simple PHP Pagination script
You also don't need to do all the calculation on your own, there are plany of pagination library for PHP, check look for them.
Hope it helps you a little.
I want to display images in from server. In the database you can find the description and name of the image. the images should be displayed in a div where always the newest is on top. As the number of images increase so does the number of div's. so, the oldest will be at the bottom. I have read a number of posts and forums but I was unable to get the logic and the code on how to do it. I hope you can help me with my problem. Thanks a lot for your answer.
the only code i have is the formatting of the dive's:
<div class="gallery">
<div class= "group">
<div class="images">
</div>
<div class="details">
</div>
</div>
<div class= "group">
<div class="images">
</div>
<div class="details">
</div>
</div>
<div class= "group">
<div class="images">
</div>
<div class="details">
</div>
</div>
</div>
it's something like this:
newest image | some details here
newer image | some datails here
new image | some details here
old image | some details here
EDIT 1
I combined Lauri Elias' and iamde_coder's answer. I come up with this code which works almost similar to what is wanted. the only problem is that it displays the item (image&details in a div) 4 times. How can I eliminate the three? thanks!
$image_query = mysql_query ("SELECT filename, story FROM tbl_contest ORDER BY time DESC");
while($image_data = mysql_fetch_array($image_query)){
$imageName = stripslashes(mysql_real_escape_string($image_data['filename']));
$imageDetails = stripslashes(mysql_real_escape_string($image_data['story']));
$count = 0;
foreach($image_data as $imageName) {
echo '<div class="group">';
echo '<div class="images"><img src="/Mainfolder/image_entry/'.$imageName.'"></img></div>';
echo '<div class="details">'.$imageDetails.'</div></div>';
$count ++;
}
}
Now that you've given a little more code to help try using this:
$count = 0;
$image_query = mysql_query ("SELECT filename, story FROM tbl_contest ORDER BY time DESC") or die(mysql_error());
while($image_data = mysql_fetch_array($image_query)){
$imageName = stripslashes(mysql_real_escape_string($image_data['filename']));
$imageDetails = stripslashes(mysql_real_escape_string($image_data['story']));
$count++;
echo '<div class="group">';
echo '<div class="images"><img src="/Mainfolder/image_entry/'.$imageName.'" alt="Image '.$count.'" /></div>';
echo '<div class="details">'.$imageDetails.'</div></div>';
}
You could, for example, use a MySQL select somewhat like this:
SELECT name, description FROM my_database.images ORDER BY created_at DESC;
Then bind the result set to a variable in PHP like $images and then iterate on it and generate HTML like this:
foreach($images as $image) {
echo '<div class="group">';
echo '<div class="images"><img src="/images_folder/'.$image['name'].'"></img></div>';
echo '<div class="details">'.$image['description'].'</div></div>';
}
im using turn.js and custome php code to load joomla articles of a specific category to make a flipbook... its working fine till i have one article for one page. I add/edit article then save it and then view it on book page... this is a tedious task to do... now suppose i have all the content of a book in a joomla article and i want to dynamically load a portion of it as a page to make the book page and so on make pages till end of article... this seems doable if i only have text with fix no of lines. But the problem is the content can have images and headings as well... so how can i dynamically vary the portion of the article to be extracted for a single page based on contents to avoid overflow of book page...
here's the working code of the book where i have one article for one page...
<?php
//get total pages code from uncategorized (default) category. (id=2)
//state 1 means published -2 for trashed
$qry="SELECT count(*) FROM `sdad_content` WHERE `catid`= '2' and `state` ='1' ";
$get_db->setQuery($qry);
$total_pages=$get_db->loadResult();
if($total_pages){
$qry="SELECT `id` FROM `sdad_content` WHERE `catid`= '2' AND `state`='1' ORDER BY `id`";
//echo "<< $qry >>";
$get_db->setQuery($qry);$get_db->query();
$total=$get_db->getNumRows();
if ($total >= 1 ) { //execute this only if we have some results
$ids = $get_db->loadAssocList();
}
}
//echo $total_pages;
for($i=0; $i<$total_pages; $i++){
$qry="SELECT `introtext` FROM `sdad_content` WHERE `catid`= '2' AND `id`= '".$ids[$i]['id']."'";
$get_db->setQuery($qry);
$cover = $get_db->loadResult();
if($i%2==0){
$class='even';
$pnoclass="pnoright";
}else{
$class='odd';
$pnoclass="pnoleft";
}
echo '<div class="text '.$class.' "> <div class="page"> '.$cover;
echo '<div class="'.$pnoclass.' "> </div>';
echo '</div> </div>';
}
//code for back cover
//adjusting the last odd page if ends at odd insert a page to make it even
if($total_pages%2!=0){
echo '<div class="text odd "> <div class="page">';
echo '<div class="pnoleft"></div>';
echo '</div> </div>';
}
echo '<div class="text even "> <div class="page"> '.$covers[1]["introtext"];
echo '<div class="pnoright"></div>';
echo '</div> </div>';
echo '<div class="cover"> '.$covers[3]['introtext'].' </div>';
?>
I strongly recommend you to simplify code, so many mysql queries for nothing...
My offer (works the same way):
<?php
$qry="SELECT `introtext` FROM `sdad_content` WHERE `catid`= '2' AND `state`='1' ORDER BY `id`";
$get_db->setQuery($qry);
$results = $get_db->loadObjectList();
if (count($results)){
foreach($results as $key => $item){
if($key%2==0){
$class='even';
$pnoclass="pnoright";
} else {
$class='odd';
$pnoclass="pnoleft";
}
echo '<div class="text '.$class.' "> <div class="page"> '.$cover;
echo '<div class="'.$pnoclass.' "> </div>';
echo '</div> </div>';
}
}
?>
Less code, only 1 query (insted of N) and same result :)
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>";