how i can diplay GROUP_CONCAT values in php - php

sqlQueryImage ThisWhatIgetinPHP
i have tried this but it keeps displaying me just the first image of every article i don't know how display multiple image for one article
$result = $db->prepare("SELECT article.titreArticle, article.textArticle,
article.idArticle, article.dateArticle, GROUP_CONCAT(images.images) AS IMG
FROM article LEFT JOIN images
ON article.idArticle = images.idArticle
GROUP BY article.idArticle");
for ($i = 0;
$row = $result->fetch();
$i++){
?>
<h1 style="text-align: center"><?php echo $row['idArticle']; ?></h1>
<hr>
<img style="width: 300px" src="images/<?php if($row ['IMG'] !=null ) {
$value = $row['IMG'];
}
$values = explode(",", $value);
echo $values[0];
?>">

The aim of GROUP_CONCAT is to return each value within each GROUP, concatenated in the same field.
You expect to have several images for certain groups, however you only display one image, the first one encountered : echo $values[0];
If you want to display all images of a group, you have to make a second loop on your array of images, to print each element.
So you have your main loop on row, and a second loop on images.
Try this:
<?php
$result = $db->prepare("SELECT article.titreArticle, article.textArticle,
article.idArticle, article.dateArticle, GROUP_CONCAT(images.images) AS IMG
FROM article LEFT JOIN images
ON article.idArticle = images.idArticle
GROUP BY article.idArticle");
// FIRST LOOP ON ROWS
for ($i = 0; $row = $result->fetch(); $i++){
?>
<h1 style="text-align: center"><?php echo $row['idArticle']; ?></h1>
<hr>
<?php
$images = ""; // we will store all our images for this row here (if any)
if($row ['IMG'] !=null ) {
$value = $row['IMG'];
$values = explode(",", $value);
// SECOND LOOP ON IMAGES
foreach($values, $imgsrc){
$images .= '<img style="width: 300px" src="images/'.$imgsrc.'">';
}
echo $images; // output all images
}
?>

Related

Get two results while looping trough associative array in PHP

I want to get two results at a time when using while looping trough a associative array in PHP.
I need to echo two results per row, something like this:
<?
while($row = mysqli_fetch_assoc($result)) {
$client_name = $row['client_name'];
$review = $row['review'];
echo('
<div class="row">
<div>
<p>'.$client_name.'</p>
<p >'.$review.'</p>
</div>
<div>
<p>'.$client_name.'</p>
<p >'.$review.'</p>
</div>
</div>
');
}
}
?>
Right now it's giving me the same result twice rather than the next one.
You can use a counter to control the output of the outside div so that you get two inside div output for each outside one:
$i = 0;
while($row = mysqli_fetch_assoc($result)) {
$client_name = $row['client_name'];
$review = $row['review'];
if ($i % 2 == 0) echo '<div class="row">';
echo '<div><p>'.$client_name.'</p><p>'.$review.'</p></div>';
if ($i % 2 == 1) echo '</div>';
$i++;
}

How to show html images witg dynamic id

I'm kind of newbie on this and I need some more expert help. The thing is, I'm trying to use this pure css easy slider but the example seems to be used with static images.
In my case I'm using php to dynamically show the images so I need to use some kind of loop (I supose) to automatically generate the id's for these tags
What I want to do is the same thing at the example but with individual ids:
<img id="img-1" src="img.jpg" alt>
<img id="img-2" src="img.jpg" alt>
<img id="img-3" src="img.jpg" alt>
<img id="img-4" src="img.jpg" alt>
<img id="img-5" src="img.jpg" alt>
So I'm trying with this code:
<?php
$categories = $conexion->query("SELECT COUNT(id) AS total,id,icon,name,image FROM categories ORDER BY id LIMIT 8");
while($row = $categories->fetch_array()) {
$id = $row['id'];
$image = $row['image'];
$name = $row['name'];
$total = $row['total'];
echo '<div id="slider">';
for($i = 0; $i <= $total; $i++) {
echo '<figure>
<img class="ns-img" id="img-'.$i.'" src="'.$image.'">
</figure>';
}
echo '</div>';
}
$categories->close();
?>
But seems that it doesn't work properly because it just show me the same first image over and over again, and the css structure just broke when these images are dynamically showed :C What could I do?
Sine you have a while loop, there is no need for the for loop
Also as in the sample you shared in your question, you only need 1 <div id="slider"> and <figure>. So you need to move them out of your loop
Your final code should look something like this:
<?php
$categories = $conexion->query("SELECT COUNT(id) AS total,id,icon,name,image FROM categories ORDER BY id LIMIT 8");
$i = 1;
echo '<div id="slider"><figure>';
while($row = $categories->fetch_array()) {
$id = $row['id'];
$image = $row['image'];
$name = $row['name'];
$total = $row['total'];
echo '<img class="ns-img" id="img-'.$i.'" src="'.$image.'">';
$i++;
}
echo '</figure></div>';
$categories->close();
?>

php mysql while loop inside while loop querying same table

This is my code
<?php
$query= "SELECT album_name FROM gallery group by album_name order by MIN(date_time)";
$stmt = $connection->prepare($query);
$stmt->execute();
$result=$stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_BOTH)) { //First while loop
$album_names =htmlspecialchars($row['album_name']);`
?>
<ul>
<li data-tags="<?php echo $album_names ?>">
Till this point everything works fine , It shows me the names of all the albums that are there in my db .
What I want to do next is I want it to show all the pics which belong to $album_names so I wrote this code after above code
// Code continues
<?php
$query2= "SELECT imgage_path,resized_path FROM gallery WHERE album=?";
$stmt = $connection->prepare($query2);
$stmt->bind_param("s",$album_names);
$stmt->execute();
$result2=$stmt->get_result();
while ($row2 = $result2->fetch_array(MYSQLI_BOTH)) { //Second while loop
$thumbnail_path = =htmlspecialchars($row2['resized_path']);
$image_path =htmlspecialchars($row2['image_path']);
?>
<a href="<?php echo $image_path ?>">
<img src="<?php echo $thumbnail_path ?>" alt="Illustration" />
</a>
</li>
</ul>
<?php
} //Second While loop ends
} //First While loop ends
?>
Problem is that it only shows me the first result of $image_path and $thumbnail_path but I want it to show all the image paths belonging to that particular album.
Hope I have cleared my question .
This looks like pseudo-code to me (as it is not having proper php tags). Anyway, Problem is as follows.. Your html tags li arrangements are wrong. That is why you are not seeing the other results. It has to be fixed as below
<?php
$query= "SELECT album_name FROM gallery group by album_name order by MIN(date_time)";
$stmt = $connection->prepare($query);
$stmt->execute();
$result=$stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_BOTH)) { //First while loop
$album_names =htmlspecialchars($row['album_name']);`
?>
<ul>
<li data-tags="<?php echo $album_names ?>">
<?php
$query2= "SELECT imgage_path,resized_path FROM gallery WHERE album=?";
$stmt = $connection->prepare($query2);
$stmt->bind_param("s",$album_names);
$stmt->execute();
$result2=$stmt->get_result();
while ($row2 = $result2->fetch_array(MYSQLI_BOTH)) { //Second while loop
$thumbnail_path = =htmlspecialchars($row2['resized_path']);
$image_path =htmlspecialchars($row2['image_path']);
?>
<a href="<?php echo $image_path ?>">
<img src="<?php echo $thumbnail_path ?>" alt="Illustration" />
</a>
<?php
} //Second While loop ends
?>
</li>
</ul>
<?php
} //First While loop ends
?>
Simplifying to a single query. This assumes that image_path and resized path always both exist for each image (if they don't it is easily fixed but this shows you the basics).
<?php
// Query gets one row per album. Row has 3 fields, album name, then all the image_path fields concatentated together, the all the resized_path fields joined together
$query= "SELECT album_name
GROUP_CONCAT(image_path ORDER BY image_path) AS image_path_concat,
GROUP_CONCAT(resized_path ORDER BY image_path) AS resized_path_concat
FROM gallery
GROUP BY album_name
ORDER BY MIN(date_time)";
$stmt = $connection->prepare($query);
$stmt->execute();
$result=$stmt->get_result();
// Start of unordered list of album names
echo "<ul>";
while ($row = $result->fetch_array(MYSQLI_BOTH))
{
$album_names = htmlspecialchars($row['album_name']);
// List item of album name
echo "<li data-tags='$album_names'>";
// Explode out the image paths into an array
$image_path = explode(',', $row['image_path_concat']);
// Explode out the image resized paths into an array
$resized_path = explode(',', $row['resized_path_concat']);
// Start of unordered list of image paths
echo "<ul>";
// Loop around the array of image paths
foreach($image_path AS $key=>$value)
{
// Assuming that the list of image paths always match the array of resized paths then use the key of the current
// image_path for both the image path and also the resized / thumbnail path
$image_path = htmlspecialchars($image_path[$key]);
$thumbnail_path = htmlspecialchars($resized_path[$key]);
echo "<li><a href='".$image_path."'><img src='".$thumbnail_path."' alt='Illustration' /></a></li>";
}
// End of unordered list of image paths
echo "</ul>";
// End of list item of album name
echo "</li>";
} //First While loop ends
// End of unordered list of album names
echo "</ul>";
?>
I do not know if my answer will help you.
But i think there is wrong in your loop for the html tag.
<?php
$query2= "SELECT imgage_path,resized_path FROM gallery WHERE album=?";
$stmt = $connection->prepare($query2);
$stmt->bind_param("s",$album_names);
$stmt->execute();
$result2=$stmt->get_result();
while ($row2 = $result2->fetch_array(MYSQLI_BOTH)) { //Second while loop
$thumbnail_path = =htmlspecialchars($row2['resized_path']);
$image_path =htmlspecialchars($row2['image_path']);
?>
<a href="<?php echo $image_path ?>">
<img src="<?php echo $thumbnail_path ?>" alt="Illustration" />
</a>
</li> <- you loop the close <li> tag
</ul> <- you loop the close <ul> tag
<?php
} //Second While loop ends
} //First While loop ends
?>
You loop the close tags (li and ul) in the second loop but you do not put open tag for the li and ul.
I hope this help. thankyou

Get the ID of my clicked product

I'm new to this site and wondering if somebody could help. I'm creating a website using bootstrap. I added some products in my DB and have used a while loop to display them on the page.
If you click on one of the products it takes you too a new page which should display all the information from only that product.
if ($select_db) {
$SQL = "SELECT * FROM products";
$result = mysql_query($SQL);
?>
<?php
$c = 0;
$id = -1; // The counter
$n = 3; // Each Nth iteration would be a new table row
while ($db_field = mysql_fetch_assoc($result)) {
$itemName = $db_field['name'];
$itemDescription = $db_field['description'];
$itemPrice = $db_field['price'];
$myPic = $db_field['image_name'];
if ($c % $n == 0 && $c != 0) { // If $c is divisible by $n...
echo '<div class="row" ></div>';
}
$c++;
$id++;
?>
<div class="col-md-4 col-sm-4" style="background-color:lavender;" id = 1>
<a href="productInfo.php">
<p> <?php echo $c ?> </p>
<h2> <?php echo $itemName ?> </h2>
<p><img class="img-responsive" img src= '<?php echo $myPic ?>' alt="Oops, Image cannot be found!" height="300" width="300"/></p>
<h3><?php echo $itemDescription ?></h3>
<p><?php echo $itemPrice ?></p>
<div id="selector" class="btn-group">
<button type="button" class="btn btn-primary" id="<?php $id ?>">Add</button>
</div>
<?php
$productsArray[] = array(
"id" => $id,
"itemName" => $itemName,
"itemDescription" => $itemDescription,
"price" => $itemPrice
);
//$_SESSION['sessionArray']=$productsArray;
?>
</a>
</div>
<?php
Now when I click on one of the columns it takes me to productInfo.php. I'm stuck as to how to display the product that is clicked?
I have added a variable $id which is the same as the array, example array [0] has id of 0, array [1] has id of 1. I think I'm trying to say if ID = 0 then display the results in the array at place [0]. I'm not sure if this helps?
I tried to display just the $id for now on productInfo.php but it only shows the last $id in the array
<?php
session_start();
if (isset($_SESSION["id"])) {
$newID = $_SESSION["id"];
echo $newID;
}
?>
I know this because it doesn't know which one I'm selecting. Am I making this too complicated?
Any help would be appreciated greatly!
Within your while loop I would add:
View More
This would concatenate the value of $id++ into the href under each item. Then all you need to do is create a landing page for product_page.php, define the $id on that page again and pull the data for that product from the db. You can do this without using any arrays.
Edit:
You would define $id on your product_page.php using $_GET['id'], as it is in the url, supplied by your href above ^
A good practice to get into whilst in the development stages would be to echo $id; to see if it is echoing the correct data from your while loop. If it echo's the correct $id then you can send the $id to the url through the href.
Let me know if this works or not.
Pulling data from db of your ID:
$sql = "SELECT * FROM table WHERE id='".$myID."'";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$data1 = $row['whatever'];
echo $data1;

Display columns with rows in php table

Hey guys I need your help, I have this table
and I want to shows like this FIDDLE, really I don't know how to do this because sometimes just exist two columns(prov_name ) and sometimes exist more that two rows please help me if you can !
Hope you understand me. Thanks so much !
In this way I can be able to select data from Joomla.
$db =& JFactory::getDBO();
$query = 'SELECT prov_name FROM provprices where CA_id = '.$CA_id;
$db->setQuery($query);
$result = $db->loadObjectList();
$prov_name = $result[0];
echo $prov_name->prov_name;
First off, in order for your data to be presented like that obviously it must be grouped accordingly.
The first row is, the prov_name's, so you can use GROUP BY or you cal also do it in PHP. Based of the sample data, it should have from 1 to 6.
Then the second row is just a simple unitval and totval according to how many prov_name's.
Third is the and the rest is the grouping of the values. See Example:
$db = new PDO('mysql:host=localhost;dbname=DATABASE_NAME;charset=utf8', 'USERNAME', 'PASSWORD');
$data = array();
$results = $db->query("SELECT * from YOUR_TABLE_NAME");
while($row = $results->fetch(PDO::FETCH_ASSOC)) {
$data[$row['prov_name']][] = $row;
}
$keys = array_keys($data);
$size = count($keys);
$vals = array();
// grouping:
// if there are six (cam1 to cam6)
// then group them by cam1, ... to cam6, then repeat until theres no more left
while(count($data) > 0) {
foreach($keys as $key) {
if(!empty($data[$key])) {
$vals[] = array_shift($data[$key]);
} else {
unset($data[$key]); // remove them if empty
}
}
}
$vals = array_chunk($vals, $size); // split them by how many prov_names
?>
<table border="1" cellpadding="10">
<!-- PROV NAMES -->
<tr><?php for($x = 1; $x <= $size; $x++): ?>
<th colspan="2"><?php echo "prov_name $x"; ?></th>
<?php endfor; ?></tr>
<!-- unitval totvals -->
<tr><?php for($x = 1; $x <= $size; $x++): ?>
<td>unitval</td><td>totval</td>
<?php endfor; ?></tr>
<!-- the grouped values -->
<?php foreach($vals as $val): ?>
<tr>
<?php foreach($val as $v): ?>
<td><?php echo $v['unitval']; ?></td>
<td><?php echo $v['totval']; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
I think you must do this first,
try looping to show prov_name in horizontal,
and then you fetch again in query with
"SELECT * FROM table_test where prov_name = '$prov_name'"
in Variable $prov_name you must have a value with CAM2 or CAM3 .etc
sorry just a logic :)

Categories