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();
?>
Related
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
}
?>
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;
How can i add a pagination system to this simple item display? And how i can add pagination for the results from filter? I just get lost that part and i cant figure it out!
I want to apply CSS on the menu too!
Here's the code:
<?php
include('db.php');
if(isset($_POST['filter']))
{
$filter = $_POST['filter'];
$result = mysql_query("SELECT * FROM products where Product like '%$filter%' or Description like '%$filter%' or Category like '%$filter%'");
}
else
{
$result = mysql_query("SELECT * FROM products");
}
while($row=mysql_fetch_assoc($result))
{
echo '<li class="portfolio-item2" data-id="id-0" data-type="cat-item-4">';
echo '<div>
<span class="image-block">
<a class="example-image-link" href="reservation/img/products/'.$row['imgUrl'].'" data-lightbox="example-set" title="'.$row['Product'].'""><img width="225" height="140" src="reservation/img/products/'.$row['imgUrl'].'" alt="'.$row['Product'].'" title="'.$row['Product'].'" />
</a>
</span>
<div class="home-portfolio-text">
<h2 class="post-title-portfolio"><font color="#666666">'.$row['Product'].'</font></h2>
<p class="post-subtitle-portfolio"><font color="#666666">Descrição: '.$row['Description'].'
<p class="post-subtitle-portfolio"><font color="#666666">Categoria: '.$row['Category'].'
<p class="post-subtitle-portfolio">Código: '.$row['Price'].'</p><br/></font></p>
</div>
</div>';
echo '</li>';
}
?>
EDITED:
<?php
include('db.php');
if(isset($_POST['filter']))
{
$filter = $_POST['filter'];
$result = mysql_query("SELECT * FROM products where Product like '%$filter%' or Description like '%$filter%' or Category like '%$filter%'");
}
else
{
$start=0;
$limit=6;
if(isset($_GET['id']))
{
$id = $_GET['id'];
$start = ($id-1)*$limit;
}
$result = mysql_query("SELECT * FROM products LIMIT $start, $limit");
}
while($row = mysql_fetch_array($result))
{
echo '<li class="portfolio-item2" data-id="id-0" data-type="cat-item-4">';
echo '<div>
<span class="image-block">
<a class="example-image-link" href="reservation/img/products/'.$row['imgUrl'].'" data-lightbox="example-set" title="'.$row['Product'].'""><img width="225" height="140" src="reservation/img/products/'.$row['imgUrl'].'" alt="'.$row['Product'].'" title="'.$row['Product'].'" />
</a>
</span>
<div class="home-portfolio-text">
<h2 class="post-title-portfolio"><font color="#666666">'.$row['Product'].'</font></h2>
<p class="post-subtitle-portfolio"><font color="#666666">Descrição: '.$row['Description'].'
<p class="post-subtitle-portfolio"><font color="#666666">Categoria: '.$row['Category'].'
<p class="post-subtitle-portfolio">Código: '.$row['Price'].'</p><br/></font></p>
</div>
</div>';
echo '</li>';
}
echo "</ul>";
$rows = mysql_num_rows(mysql_query("SELECT * FROM products"));
$total = ceil($rows/$limit);
if($id>1)
{
echo "<center><a href='?id=".($id-1)."' class='button'>Anterior</a></center>";
}
if($id!=$total)
{
echo "<center><a href='?id=".($id+1)."' class='button'>Próximo</a></center>";
}
?>
You should have something like this:
// declare a base query
$q = "SELECT * FROM products";
if(isset($_POST['filter']))
{
$filter = $_POST['filter'];
// append filter to query
$q += "where Product like '%$filter%' or Description like '%$filter%' or Category like '%$filter%'");
}
// check for "page" URL parameter, if not available, go to first page
$page = isset($_GET['page']) ? $_GET['page'] : 1;
// check for "pageSize" URL parameter, if not available, fall back to 20
$pageSize = isset($_GET['pageSize']) ? $_GET['pageSize'] : 20;
// append the pagination to your query
$q += sprintf("LIMIT %d,%d;", ($page-1)*$pageSize, $pageSize);
// execute the constructed query
$result = mysql_query($q);
Note that the code is "pseudoish", not tested, but should give you the base idea.
Also, you can check this SO post about pagination with MySQL.
UPDATE
In PHP if you use an uninitialized variable, then it will have a context-dependent default value. See the documentation about this here. Here is an extract:
It is not necessary to initialize variables in PHP however it is a
very good practice. Uninitialized variables have a default value of
their type depending on the context in which they are used - booleans
default to FALSE, integers and floats default to zero, strings (e.g.
used in echo) are set as an empty string and arrays become to an empty
array.
I have this:
<a href="">
<img class="img" src="
<?php $query = mysql_query("
SELECT * FROM posts WHERE ID = 49");
while($row = mysql_fetch_array( $query ))
{ echo $row['Image1(170x170)']; }
?>" width="180px" height="130px">
</a>
I want to echo an image path where I have the echo that I have stored the path into a row in a database... In generally I have stored the image path like this: ../folder/folder/file.jpg for another reason that I cant changed it and now I want to pull that from the database and echo it here but I want to change and done like this from
../folder/folder/file.jpg to folder/folder/folder/file.jpg
Is there a way to do that?
Because I have searched for a lot of time and I have only find the REPLACE() that I don't want to use because I don't want to change my records in my database. Any help would be appreciated! Thanks in advance!
You can do it in the PHP code:Remove the .. from the beginning of the string using substr and add the "folder" instead:
echo "folder" . substr($row['Image1(170x170)'],2);
I want to change and done like this from ../folder/folder/file.jpg to folder/folder/folder/file.jpg
The basic idea here is to get your image src link, and trim the first two .. characters. This can be done in many ways. I've just str_replace() in the below code:
$query = mysql_query("SELECT * FROM posts WHERE ID = 49");
while($row = mysql_fetch_array( $query ))
{
$src = $row['Image1(170x170)'];
$src = str_replace('..', '', $src);
?>
<a href=""><img class="img" src="<?php echo $src; ?>" width="180px" height="130px"/>
<?php
}
I have to tables:
gallery -> id_gallery, name, desc, data
and:
images -> gallery_id, name
One gallery can have many images. I need to select all galleries and all images and on view page present all galleries and all images which contains that gallery. How can do this?
I have code like this for now:
$this->db->select('*')->from('image')->join('gallery','id_gallery = gallery_id');
$q = $this->db->get();
return $q = $q->result_array();
EDIT:
<?php foreach ($gallery as $gal): ?>
<figure>
<a href="<?php echo IMG ?>galerija/<?php echo $gal['name'] ?>/<?php echo $gal['path'] ?>" rel="galerija[slike]" class="figure_img">
<img src="<?php echo IMG ?>galerija/<?php echo $gal['naziv'] ?>/thumbs/<?php echo $gal['path'] ?>" >
<figcaption><?php echo $gal['name']; ?></figcaption>
</a>
</figure>
<?php endforeach; ?>
This foreach loop is producing 5 div tags instead of one (if gallery have 5 images for example).
If you want to select all the images from images table for all galleries..........
$this->db->select('*');
$this->db->from('images');
$this->db->join('gallery', 'gallery.id_gallery = images.gallery_id');
$query = $this->db->get();
return $q = $query->result_array();
or in the other way. Write a model function and in that
public function get_images()
{
$res=$this->db->query("select * from images and gallery where gallery.id_gallery = images.gallery_id");
if($res->num_rows()>0){
return $res->result("array");
}
return array();
}