This is my first time posting here so please bear with me.
I'm creating an image gallery using HTML & CSS, where I show one image on the screen and it has a prev/next button to toggle through the images (at no point does this take you to a new webpage). I'm using PHP & MySQL to populate the images.
So, I have a table like this:
| ID | Name | Genre |
--------------------------------
| 1 | Apple | Fruit |
--------------------------------
| 2 | Cabbage | Veg |
--------------------------------
| 3 | Lettuce | Veg |
--------------------------------
| 4 | Pear | Fruit |
--------------------------------
| 5 | Banana | Fruit |
--------------------------------
| 6 | Leek | Veg |
--------------------------------
| 7 | Kiwi | Fruit |
I want to only images where genre=fruit, so now my table is like the following:
| ID | Name | Genre |
--------------------------------
| 1 | Apple | Fruit |
--------------------------------
| 4 | Pear | Fruit |
--------------------------------
| 5 | Banana | Fruit |
--------------------------------
| 7 | Kiwi | Fruit |
Since the IDs are no longer incremental, I can no longer simply use $id-1 or $id+1 to get the prev/next photos.
This is what my PHP code looks like (simplified for this post):
<?php
$sql = "SELECT * FROM photo WHERE genre LIKE '%fruit%' ORDER BY id DESC";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while ($rows = mysql_fetch_assoc($result)) {
$id=$rows['id'];
$name=$rows['name'];
$genre=$rows['genre'];
echo "<div id=\"img$id\">".
"<".
">".
"<img src=\"img/full/$name.jpg\">".
"</div>";
} // end while
?>
Since I'm loading all of the fruit photos, I don't know what is the current ID and I won't know how many rows there will be displayed in total. I'd like to find the ID of the row item that is before and after the current row/photo the user sees.
I've tried using current(), next(), and prev() but they all seem to select the first 3 rows only. Any help would be so greatly appreciated! Been researching this all day and haven't been able to solve this :( Thanks so much in advance.
Here's a sample code with comments:
$images = [];
while ($rows = mysql_fetch_assoc($result)) {
// first we store images in an array
$images[] = [
'id' => $rows['id'],
'name' => $rows['name'],
'genre' => $rows['genre'],
];
}
// next we iterate over this array
foreach ($images as $index => $image) {
echo '<div id="img' . $image['id'] . '">';
// as array is numerically indexed
// we can get previous item's index as `$index - 1`
// and check if it is set
if (isset($images[$index - 1])) {
// if it is set - we output it
echo '<';
}
// as array is numerically indexed
// we can get next item's index as `$index + 1`
// and check if it is set
if (isset($images[$index + 1])) {
// if it is set - we output it
echo '>';
}
echo '<img src="img/full/' . $image['name'] . '.jpg">';
echo '</div>';
}
Related
i want to fetch multiple columns of mysql table for each unique id and echoing the result in a single row of a HTML table for each unique id.I am getting the result in the following form from the table ..
userid | date | status
------------------------------------
1 | 28-06-17 | 1
------------------------------------
1 | 29-06-17 | 2
------------------------------------
1 | 30-06-17 | 0
------------------------------------
2 | 28-06-17 | 1
------------------------------------
2 | 29-06-17 | 2
------------------------------------
2 | 30-06-17 | 3
------------------------------------
Here is the code..just a basic php code to get the record from database..
if($con)
{
$query="SELECT * FROM `usersDetail` WHERE userid='$_POST["user_id"]'";
$result=mysqli_query($con, $query);
while($rowusersDetail=mysqli_fetch_assoc($result))
{
echo $rowusersDetail['userid']."<br>";
echo $rowusersDetail['date']."<br>";
echo $rowusersDetail['status']."<br>";
}
}
*what i want is to generate html table using PHP to display all the Records of a
particular user into a single row..
userid | 28-06-17 | 29-06-17 | 30-06-17
_________________________________________
1 | 1 | 2| 0
_________________________________________
2 | 1 | 2| 3
_________________________________________
kindly please Help..thanks
This is can be done with the help of foreach loop.
For that you need to retrieve all data at a time then you can manipulate the data with your requirement.
You can do like this
If your data is like
$data=[['userid'=>1,'date'=>'21-03-2017','status'=>2],
['userid'=>2,'date'=>'21-03-2017','status'=>1],
['userid'=>1,'date'=>'22-03-2017','status'=>6],
['userid'=>2,'date'=>'22-03-2017','status'=>7],
['userid'=>1,'date'=>'24-03-2017','status'=>2],
['userid'=>2,'date'=>'24-03-2017','status'=>6]];
Then
$finalData=[];
foreach ($data as $stats){
$finalData[$stats['userid']][$stats['date']]=$stats['status'];
}
foreach ($finalData as $id=>$value){
echo 'userId->'.$id;
echo '<br>';
foreach ($value as $date=>$status){
echo $date.' -> '.$status; echo '<br>';
}
echo '<br>';
}
It will give out put as
I thing it will work for you.
How to show multiple record in one table row ?
I'm not good explain using words, so i'll use an example :
Work No. | Product No. | Product Name | Qty | Item No. | Item Name | Qty |
--------------------------------------------------------------------------
W00001 | P0000001 | Product_1 | 6 | I000001 | Item_1 | 2 |
| | | | I000002 | Item_2 | 2 |
| | | | I000003 | Item_3 | 2 |
--------------------------------------------------------------------------
Total : 6 |
--------------------------------------------------------------------------
W00002 | P0000002 | Product_2 | 7 | I000001 | Item_1 | 3 |
| | | | I000004 | Item_4 | 4 |
--------------------------------------------------------------------------
Total : 7 |
--------------------------------------------------------------------------
as you can see, product is made from multiple items. So far, i can make more less like example above but my question is how to display item like example above. I want to show items in one row. How to show that using php ?
If you already have 2 tables: one for products and one for items you simply have to loop throw the products one and for each product get all the items needed for that product and display there.
Your code needs to look something like this:
$products = get_products_from_db(); //How you normal get products from db.
foreach($products as $product) {
$items = get_items_by_product_from_db($product['id']); //get from db all items for a product.
//foreach product we will need a new row.
echo '<tr>';
echo "<td>{$product['work_no']}</td>"; //and all the normal cells like this.
//for item no. we will create a cell and inside it we will foreach items.
echo "<td>";
foreach($items as $item) {
echo $item['item_no'] . '<br/>';//Or how it's called in db.
}
echo "</td>";
//Exact the same with name and quantity.
//For quantity also keep a counter so you can display the total
echo '</tr>';
//After this row create a new tr to display the total. I think you know how to do this.
}
I have four columns in a properties table: property_id, value, id, material_id.
I also have an array of properties: Array $properties
The schema is a bit complicated, because I want to find the material_id based on the matching properties.
An example:
$properties = array(['property_id'=>1,'value'=>3],['property_id'=>2,'value'=>6],['property_id'=>3,'value'=>4]);
Example table output:
+----+-------------+-------------+-------+
| id | material_id | property_id | value |
+----+-------------+-------------+-------+
| 1 | 1 | 3 | 5 |
| 2 | 1 | 3 | 5 |
| 3 | 1 | 3 | 5 |
| 4 | 2 | 1 | 3 |
| 5 | 2 | 2 | 6 |
| 6 | 2 | 3 | 4 |
| 10 | 4 | 1 | 9 |
| 11 | 4 | 2 | 3 |
| 12 | 4 | 3 | 6 |
+----+-------------+-------------+-------+
Now, I need material_id that satisfies all the properties. How can I do that..? Do I need to use exist statement of MySQL?
Now, for each element in your array you will want to run a statement that looks like this:
SELECT material_id FROM properties WHERE property_id = 2 AND value = 3;
Do you need help on the php code also? You could run a for each loop, but I will need to know what way you are using to communicate with your database for more specifics.
edit
foreach ($properties as $foo => $bar)
{
$sql = 'SELECT material_id FROM properties WHERE ';
foreach ($bar as $key => $value)
{
$sql .= $key .' = '. $value .' AND ';
}
$sql .= 'true';
*run your PDO code on $sql here*
}
On behalf of performance, it's not a good idea to run a query per array's value. If you have an oversized array things can get pretty slower.
So, best solution can be to build a single query including all conditions presented on $properties array:
<?php
$properties = array(['property_id'=>1,'value'=>3],['property_id'=>2,'value'=>6],['property_id'=>3,'value'=>4]);
$qCondition = [];
foreach($properties as $prop) {
$q = sprintf("(property_id = %d AND value = %d)", $prop["property_id"], $prop["value"]);
$qCondition[] = $q;
}
// assuming that your database table name is 'materials'
$sql = sprintf("SELECT * FROM materials WHERE (" . implode(" OR ", $qCondition) . ")");
echo $sql;
Result:
SELECT * FROM materials
WHERE ((property_id = 1 AND value = 3) OR (property_id = 2 AND value = 6) OR (property_id = 3 AND value = 4))
Therefore, you need to run only one single query to get all desired rows.
You can play with suggested solution here: http://ideone.com/kaE4sw
I need to search following result .
singer_id=2
---------
B9 - song's list
song_au1
song10
p7 - song's list
songx
song11
11T- song list
song 35
The result is actully have following requirements.
1. show all songs by singer_id=2 order by (custom arrangement may be some or other table).
and because i'm using PHP, I want to show result the above format..
top - singer name
then it's album type
and all songs of that album
2nd album type
and all songs of that album
.....
I'm too much confused..
can somebody please tell me the best query!
table i've
================================================
| u_id | u_title | u_song_type | singer_id |
|================================================
| 1 | song1 | p7 | 1 |
| 2 | songx | p7 | 2 |
| 3 | songpo | p7 | 9 |
| 4 | song_dum3| 11T | 2 |
| 5 | song_01 | p7 | 3 |
| 6 | song_au1 | B9 | 2 |
| 7 | song11 | p7 | 2 |
| 8 | song35 | 11T | 1 |
| 9 | song10 | B9 | 2 |
-------------------------------------------------
Thank
After a long effort of hours. I have resolved it.
At the end I got It was easy.
$artistId = '2';
echo 'Singer Id:' . $row["artistName"];
//following query selects all records and sort records in a why i need that.
$sql2 = "SELECT * FROM album
ORDER BY
FIELD(u_song_type, 'B9','p7','11T')";
$result2 = mysqli_query($con,$sql2);
//An array is defined, to filter the title only once if
//repeated occurrence
$songTypesArray = array();
while($row2 = mysqli_fetch_array($result2)) {
if (in_array($row2['u_song_type'], $songTypesArray)) {
//if song Type is found in the array, skip it.
//I left it empty. if found in array
}
else{
//if song type is not found in the arry we'll push
// song type into array
array_push($songTypesArray,$row2['u_song_type']);
// song type only appears once if it is same
// in all song title.
echo $row2['u_song_type'];
}
//at the end song title appears.
echo $row2["u_title"];
}
}
This took whole night of mind in resolution and finally it is there. I hope people would get enough help from here
Thanks
Hello I have actually asked a similar question a while ago but only just realized I did not get an answer that solves my problem.
I have 2 tables in a MySQL DB, that are connected by the same main id, the following code is just a simplified example of the original code:
table1:
+-----------------------+
| main_id | name | type |
+-----------------------+
table2:
+----------------------------------------+
| main_id | secondary_id | color | price |
+----------------------------------------+
the result I want to achieve is to get every row of table 1, and under each one list all the linked by main id rows from table2, for example:
table1 rows:
+-------------------+
| 1 | name1 | type1 |
| 2 | name2 | type2 |
| 3 | name3 | type3 |
+-------------------+
table2 rows:
+----+------+----------+------+
| 1 | 23 | red | 500 |
| 1 | 24 | blue | 600 |
| 1 | 25 | green | 700 |
| 2 | 26 | pink | 400 |
| 2 | 27 | purple | 200 |
| 3 | 28 | white | 100 |
+----+------+----------+------+
result in display:
<h1 class="1">name1, type1</h1>
<div class="23">red,500</div>
<div class="23">red,500</div>
<div class="24">green,700</div>
<h1 class="2">name2, type2</h1>
<div class="25">pink,400</div>
<div class="26">purple,200</div>
And so on...I was using a while loop inside a while loop which wasn't giving me the required result, then I was advised to use MySQL JOIN, but when I do I get all values of the matching ids and cant display the headings only once and then list the related results under it, can someone please help me?
this is a simplified query i was using:
while($rows = $headings->fetch_array(MYSQLI_BOTH))
{
$id = $rows['id'];
$conts_q = $mysqli->query("SELECT * FROM `table2` WHERE id='$id'");
$conts_numr = $conts_q->num_rows;
if($conts_numr==0)
{
//Display empty
}
else
{
while($row2 = $conts_q->fetch_array(MYSQLI_BOTH))
{
//Get details and display
}
}
}
In your description, you use main_id, but in the code you use just id. Not sure which you're really using here - you will have to edit the code below to match your actual column names. If the database column name is actually main_id, then for instance the line with $id = $rows['id'] would have to be $rows['main_id']. Or it won't work.
while($rows = $headings->fetch_array(MYSQLI_BOTH))
{
echo '<h1 class="'.$rows['id'].'">'.$rows['name1'].', '.$rows['type'].'</h1>';
$id = $rows['id'];
$conts_q = $mysqli->query("SELECT * FROM `table2` WHERE id='$id'");
$conts_numr = $conts_q->num_rows;
if($conts_numr==0)
{
//Display empty
}
else
{
while($row2 = $conts_q->fetch_array(MYSQLI_BOTH))
{
if ($row2['id'] == $id) {
echo '<div class="'.$row2['secondary_id'].'">'.$row2['color'].', '.$row2['price'].'</div>';
}
}//while
}//else
}//while