displaying images from mysql in php in rows and cols - php

i need to display images from mysql in my gallery. i have a responsive template. i have around 40 images in my mysql i want to fetch images and display it in gallery view of 4 column per row and it should go to the next set
here is my code for the html gallery
<ul class="row box2">
<li class="span4">
<img></img>
</li>
<li class="span4">
<img></img>
</li>
<li class="span4">
<img></img>
</li>
<li class="span4">
<img></img>
</li>
</ul>
so above is my code i need to repeat this code for every 4 image i show from Mysql using php so that i can form a gallery
pls help in looping this for every four image fetched so that i disaplay all my 40 images in 10 rows 4 per column
thanks in advance

You can do it like this
$i=0;
while = ($row = mysql_fetch_row($resource)){
if($i==0){
?><ul class="row box2"><?php
}
?>
<li class="span4"><img src='<?php echo $row['image']?>'></img></li>
<?php
$i++;
if($i==4){
?></ul><?php
$i=0;
}
}

Use array_chunk to devide you main Array with all images into small array with 4 images
as example:
$mainArray;// here the value of all images from db
$resultArray = array_chunk($mainArray, 4); //now you have arrays which include 4 images
And then use foreach to build block as you want
<?php foreach ($resultArray as $minArray){ ?>
<ul class="row box2">
<?php foreach ($minArray as $array){ ?>
<li class="span4">
<img><?php echo $array['img']?></img>
</li>
<?php } ?>
</ul>
<?php } ?>

I am not sure if i understand your questing correctly but you can use for loops . Is this what you want?
var x;
var y = 1;
for (var i=0;i<10;i++)
{
x = x + '<ul class="row box' + i + '">
<li class="span4">
<img> '+ imageArray[y] +' </img>
</li>
<li class="span4">
<img> '+ (imageArray[y] +1) +'</img>
</li>
<li class="span4">
<img>'+ (imageArray[y] + 2) +' </img>
</li>
<li class="span4">
<img>'+ (imageArray[y] + 3) +'</img>
</li>
</ul>';
y++;
}

Related

pagination with bootstrap 4 cards

I'm creating a sort of blog area on my website using bootstrap 4 cards, i fill my cards with data from my database. And i want to put the cards inside of a pagination with a category filter
my category select is working fine, but I'm not sure how I'm supposed to tackle the part for calculating how many rows and columns are supposed to be on the webpage. I know i can make a variable which holds the maximum amount of rows and columns and calculate the offset from that with the ceil function. Then put that into a SQL query with the LIMIT being the offset and maximum items but I am not sure how this works with rows. this is the code i have at the moment. `
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li><li class="page-item"><a class="page-link" href="blog.php?category=<?php if (isset($_GET["category"])){echo $_GET["category"];}?>&pagenumber=<?php // this is also where I'm stuck ?>">1</a>
</li>
<li class="page-item"><a class="page-link" href="blog.php?category=<?php if (isset($_GET["category"])){echo $_GET["category"];}?>&pagenumber=<?php // this is also where I'm stuck ?>">2</a></li>
<li class="page-item"><a class="page-link" href="blog.php?category=<?php if (isset($_GET["category"])){echo $_GET["category"];}?>&pagenumber=<?php // this is also where I'm stuck ?>">3</a></li>
<li class="page-item">
<a class="page-link" href="#">Next</a>
</li>
</ul>
</nav>
`
the category works so for example if i select category 2 the url will be
http://localhost/HCDistributie/blog.php?category=2&pagenumber=
the only thing that is needed is to get the pagenumber.
this is my back-end code where i loop through each each item in the database and put that inside the card
function getNews()
{
// get current category
if (isset($_GET["category"]) && is_numeric($_GET["category"])) {
// bind get category to categoryId
$categoryId = $_GET["category"];
// if category is 1
if ($categoryId == 1) {
if (isset($_GET["pagenumber"]) && is_numeric($_GET["pagenumber"])) {
$pageNumber = $_GET["pagenumber"];
try {
$itemLimits = 9;
$rowLimit = 3;
$db = new Connection();
$database = $db->openConnection();
$stm = $database->query("SELECT * FROM blog where category = $categoryId");
$stm->execute();
$rowNews = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach ($rowNews as $items) {
?>
<div class="card-group">
<div id="classContainer" class="col-md-4">
<div class="card">
<img src=" <?php $items['img'] ?>" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title"><?php echo $items["title"]; ?></h5>
<p class="card-text"><?php echo $items["content"] ?></p>
<p class="card-text"><small
class="text-muted"><?php $items["author"]; ?></small>
</p>
</div>
</div>
</div>
</div>
<?php
}
} catch
(PDOException $e) {
echo sprintf("Something went wrong. $e->errorInfo");
}
}
}
}
}
I sort of have an idea what i should do the problem is I'm not sure how to apply that to my code, the tutorials i have read only deal with variable for max items while i have 2 max columns and max rows.
I am not sure how to calculate both of them separately.
This is the first time I worked with any sort of pagination so i apologize if some things seem weird and or stupid.
This is not weird or stupid, first contact with pagination is always a hassle ;)
With bootstrap, you could get away with just knowing the maximal occurrences of your card records. You could wrap everything while using rows and cols, check out this example:
<div class="container">
<div class="row">
<div class="col-4">
1 of 5
</div>
<div class="col-4">
2 of 5
</div>
<div class="col-4">
3 of 5
</div>
<div class="col-4">
4 of 5
</div>
<div class="col-4">
5 of 5
</div>
</div>
</div>
Fiddle
Thanks to .col-4, you will always get three cards in a row. In your example:
<div class="card-group">
<div class="row">
<?php foreach ($item as $items) { ?>
<div class="col-4">
<div class="card">
// Your card content goes here, use $item
</div>
</div>
<?php } ?>
</div>
</div>
More importantly, it seems you writing something in plain PHP. If you want to go public, you should consider a framework that has already some validation capabilities and function helpers - even build in pagination! I would pick Laravel for that.
EDIT:
Fixed mistake. Thanks #hakiko

how to pass value from while loop to next page after clicking on menu item php

I have a a navigation bar and one of the nav items is a dropdown with sub categories. Only the subcategories are being pulled from the database by using a while loop.
When clicking on one of the dropdown items they will be redirected to dancerProfile.php.
I want dancerProfile.php to pull the menu item name from the other pages.
html
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
//if($res==FALSE){
//die('there was an error running query [' . $con->error . ']');
// }
while($data=mysqli_fetch_array($dres)){
$dancerName = $data['dancer_name'];
foreach ($dancerName as $DANCER){
echo '
<li>'.$data["dancer_name"].'</li>
<li class="divider"><hr></li>
';
}
}
?>
<li>Add New</li>
</ul>
</li>
This works well as all dancers appear in the dropdown.
What I want is that when I click on dancerA the dancerProfile.php will show dancerA information. And when I click on dancerB it will show dancerB info, etc.
But it will only show the last dancer's information no matter which name I click on.
dancerProfile.php
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo $dancerName;?></div>
</div>
So When I click on dancerA on the nav bar in any other page, in dancerProfile.php $dancerName should print dancerA. And it should print dancerB if I clicked on dancerB from the nav bar.
but it is only printing dancerB no matter which link I click.
I am using bootstrap. Can anyone help me?
EDIT
Here's an update of what my code looks like now.
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name`, `id` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
$dancerId= 'id';
}
while($data=mysqli_fetch_array($dres)){
$dancerName = $data['dancer_name'];
echo '
<li>'.$data["dancer_name"].'</li>
<li class="divider"><hr></li>
';
}
?>
<li>Add New</li>
</ul>
</li>
And dancerProfile.php:
<?PHP
if(isset($_GET['id'])) {
$dancerId=$_GET['id'];
$dancerquery = "SELECT `dancer_name` FROM `dancers` WHERE id = " . $_GET['id'] . ";";
$dancer_res = mysqli_query($con,$dancers);
if($dancer_res){
$DANCER='dancer_name';
}
}
?>
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo " $DANCER ";?></div>
</div>
I also forgot to mention that this navigation is also on dancerProfile page. So all of the code I am providing is on dancerProfile page. I don't know if that matters
My database table
You need pass dancer id or which is unique in your dancer table. something like given below.
<li>'.$data["dancer_name"].'</li>
And now go to dancerProfile.php and try something like this.
if (isset($_GET['dancer_id'])) {
$dancer_id=$_GET['dancer_id'];
//your query
}
Your full code:
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name`, `id` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
$dancerId= 'id';
}
while($data=mysqli_fetch_array($dres)){ ?>
$dancerName = $data['dancer_name'];
<li><?php echo $data['dancer_name']; ?>'</li>
<li class="divider"><hr></li>
<?php } ?>
<li>Add New</li>
</ul>
</li>
Your dancerProfile.php should be
<?PHP
if(isset($_GET['id'])) {
$dancerId=$_GET['id'];
$dancerquery = "SELECT `dancer_name` FROM `dancers` WHERE id = '$dancerId'";
$dancer_res = mysqli_query($con,$dancerquery);
$data=mysqli_fetch_array($dancer_res);
}
?>
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo $data['dancer_name'];?></div>
</div>
You are currently overriding the value of $DANCER with each iteration, so the last will always be the used value.
Just change your loop a bit:
while($data=mysqli_fetch_array($dres)){
echo '<li>'.$data['dancer_name'].'</li>';
}

PHP: split a mySQLi array in two divs?

I'm currently working on a page that calls a mySQL database to populate a list of members. However, I need that list to be split into two equal parts, so that half can float to the left and half to the right. However, I have beat this horse to death and still cannot figure out how to split the array.
Here's what I have currently:
<div class="holder">
<?php
$members = $db->query('SELECT * FROM tableContacts ORDER BY lastName ASC');
$rowcount = mysqli_num_rows($members);
if ( $rowcount > 0 ) { ?>
<div class="members-left">
<ul class="members">
<?php while ($row = $members->fetch_assoc()) { ?>
<li class="member">
//SOME CONTENT HERE
</li>
<?php } ?>
</ul>
</div>
<?php } ?>
</div>
However, I want the output to look something like this (let's say my table has 10 members):
<div class="holder">
<!--output first half of members from table: -->
<div class="members-left">
<ul class="members">
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
</ul>
</div>
<!--output second half of members from table: -->
<div class="members-right">
<ul class="members">
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
<li class="member">...</li>
</ul>
</div>
</div>
I've tried setting counters and using things like if($i <= $rowcount/2), but to no avail. Any help would be greatly appreciated—I'm very new to mySQL and have limited knowledge of PHP, so this one has me stumped.
add a control variable like "$i". add +1 to $i every loop. and check if $i reached half of number of rows. if reached close ul and div and open new div and ul and set $i to 0
<div class="holder">
<?php
$members = $db->query('SELECT * FROM tableContacts ORDER BY lastName ASC');
$rowcount = mysqli_num_rows($members);
$i=0;
if ( $rowcount > 0 ) { ?>
<div class="members-right">
<ul class="members">
<?php while ($row = $members->fetch_assoc()) { ?>
<li class="member">
//SOME CONTENT HERE
</li>
<?php
$i++;
if($i>=($rowcount/2)){
echo '</ul></div>
<div class="members-right">
<ul class="members">';
$i=0;
}
} ?>
</ul>
</div>
<?php } ?>
</div>
You could use a for loop for the first half and just finish the second half with a while loop that goes until there are no more results.
First half:
for($i = 0; $i <= $rowcount/2 && $row = $members->fetch_assoc(); $i++)
Second half
while ($row = $members->fetch_assoc())
Example with HTML
<div class="members-left">
<ul class="members">
<?php for($i = 0; $i <= $rowcount/2 && $row = $members->fetch_assoc(); $i++) { ?>
<li class="member">
// do something with $row
</li>
<?php } ?>
</ul>
</div>
<div class="members-right">
<ul class="members">
<?php while ($row = $members->fetch_assoc()) { ?>
<li class="member">
// do something with $row
</li>
<?php } ?>
</ul>
</div>
I would recommend you to make 2 arrays of your members and then print them out. The advantage is you will have more control of the members and your code will be more readable for later use;
<?php
$i =0;
while ($row = $members->fetch_assoc()) {
if($i % 2 == 0)
{
$right_members[] = $row;
}
else
{
$left_members[] = $row;
$i++;
}
echo '
<div class="members-right">
<ul class="members">';
foreach($right members as $r_member){
echo '<li class="member">...</li>';
}
echo '
</ul>
</div>';
//SAME WITH LEFT MEMBERS
?>
Having a counter was an idea, I will not debate on having model logic in view ;)
well, a solution would be :
<div class="holder">
<div class="members-column">
<ul class="members">
<?php
$members = $db->query('SELECT * FROM tableContacts ORDER BY lastName ASC');
$rowcount = mysqli_num_rows($members);
$i = 0;
while ($row = $members->fetch_assoc()) {
?>
<li class="member">
//SOME CONTENT HERE
</li>
<?
// Check if current row is the 'middle count', creating a new column
if ($i === ceil($rowcount / 2)) : ?>
</ul>
</div>
<div class="members-column">
<ul class="members">
<?php endif; ?>
<?php
$i++;
}
?>
</ul>
</div>
</div>

How to render a Drupal 7 menu structure with divs instead of lists

I want to change the output of the Drupal 7 menu structure which is like:
<?php print render($primary_nav); ?>
outputs:
<ul class="menu nav navbar-nav">
<li class="first expanded">
<a title="" href="lorem">Lorem</a>
<ul class="menu nav">
<li class="first leaf"><a title="" href="/lorem">Lorem</a></li>
<li class="leaf"><a title="" href="/ipsum">Ipsum</a></li>
</ul>
</li>
<li class="leaf"><a title="" href="/dolor">Dolor</a></li>
<li class="expanded"><a title="" href="/sit">Sit</a>
<ul class="menu nav">
<li class="first leaf"><a title="" href="/sit">Amet</a></li>
<li class="leaf"><a title="" href="/consectetur">Consectetur</a></li>
</ul>
</li>
</ul>
how could I have instead of the ul and li the menu rendered with divs.
Thanks
I feel curious... why do you want to do so?
I guess you should try to make a modified copy of 'render' function and call it instead.
EDIT
The print "function" in PHP just outputs the argument received. I guess you should define your own php function, namely print_menu_as_div:
function print_menu_as_div($primary_nav) {
$htmlCode = '<div class="myOwnMenuClass">';
...
/*Generate your HTML code here to display your
menu items and append to $htmlCode*/
...
$htmlCode .= '</div>';
return $htmlCode;
}
Then instad of:
<?php print render($primary_nav); ?>
write:
<?php print print_menu_as_div($primary_nav); ?>
Hope it helps ;)

Adding a class name to every fouth item in a forloop

I'm used to doing this with Django and it's fairly simple, so trying to workout how it's done in PHP. What I'd like to do is within a PHP for foreach loop I would like to add the class name of 'last' to every fourth item in the list.
PHP code:
<?php
$products = array();
$product_counter = 0;
foreach ($_productCollection as $_product)
{
?>
So this is my current HTML output:
<li>
<div class="contentWrap"> content here </div>
</li>
<li>
<div class="contentWrap"> content here </div>
</li>
<li>
<div class="contentWrap"> content here </div>
</li>
<li>
<div class="contentWrap"> content here </div>
</li>
However, what I would like to acheive is:
<li>
<div class="contentWrap"> content here </div>
</li>
<li>
<div class="contentWrap"> content here </div>
</li>
<li>
<div class="contentWrap"> content here </div>
</li>
<li>
<div class="contentWrap last"> content here </div>
</li>
I was using the nth child attribute in CSS3 but it needs to be supported in IE8!
Anyway, PHP isn't my strong point and can't make use of the examples i've found online so any help would be much appreciated.
Thanks
Use something like this:
$counter = 0;
foreach ($_productCollection as $_product) {
//whatever
if ($counter % 4 == 3) {
//do stuff for every 4th element
}
$counter++;
}
Simple right?
for($i = 0; $i < $countOfArray; $i++) {
if($i % 4 == 3) {
// add last class ....
}
}

Categories