I have a question which is very similar to this one: Display Album with Photos (PHP), however I need a bit of help in applying the code to my situation.
I have two tables, which are related by the column AlbumID:
Album: AlbumID, name, date(optional)
Image: ImageID, name, imageURL, AlbumID
My PHP code so far:
function select_galleryimage($sql,$a_id, $img_id) {
include 'connect.php';
$result = $conn->query($sql);
if ($result->num_rows > 0);
while ($row = $result->fetch_assoc()) {
echo "<div id='".$row['AlbumID']."'>\n
<a class='$a_id' href=".$row["imageURL"]." target='_blank'>
<img src=".$row["imageURL"]." alt='".$row['name']."'>
</a>\n
</div>\n";
}
}
where $sql = SELECT * FROM album JOIN image ON album.AlbumID=image.AlbumID'
$a_id = 'lightbox' and $img_id = ''
The issue that I am having is how to create a loop which creates the divs into which the loop with the images is inserted, so as the end result is something like this:
<div id="album1" style="display:none">
<h1>Gallery</h1>
<a class="lightboxX35" href="media/Photos/_MG_7732.jpg"><img src="media/Photos/_MG_7732.jpg"></a>
<a class="lightboxX35" href="media/Photos/_MG_7508.jpg"><img src="media/Photos/_MG_7508.jpg"></a>
</div>
Gallery
Thank you all in advance for any help provided.
Somehing like this?
if ($result->num_rows > 0) {
$current_album = null;
while ($row = $result->fetch_assoc()){
if ($current_album <> $row['AlbumID']) {
if (!empty($current_album))
echo "</div>\n";
echo "<div id='".$row['AlbumID']."'>\n";
echo "<h1>Gallery</h1>\n";
$current_album := $row['AlbumID'];
}
echo "<a class='$a_id' href='".$row["imageURL"]."' target='_blank'><img src=".$row["imageURL"]." alt='".$row['name']."'></a>\n";
}
echo "</div>\n";
}
I haven't tried it on a server so may not be the correct synthax...
With more than a single album you have to sort the SQL by album using ORDER BY then echo the DIV fo every new album. To achieve this assign a variable $current_album = null; outside the while loop then check for changes inside.
Related
I'm writing an html code to retrieve the details of the item table in my database.In it, I have some columns and one such column name is Image.It stores images of the Items and it's type is blob.I have written the following code to retrieve and display the data in this table in my php file.
$sql = "SELECT * FROM item where item.Category = 'Tops' limit 3";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$id = $row["Item_ID"];
echo " <div class='ite'>
<div class='card'>
<img src=".$row["Image"]." alt=".$row["Item_Name"]." class='imge'>
<h3>".$row["Item_Name"]."</h3>
<p class='price'>Rs. ".$row["Price"]."</p>
<p><button type='submit' onclick='view_data($id)'>View Item</button></p>
</div>
</div>";
}
}
The code works perfectly for the other columns except the image column(<img src=".$row["Image"]." alt=".$row["Item_Name"]." class='imge'>)
The output is as follows.
It gives garbage values like this.How can I fix this error and display the image stored in the database?
<img src='data:image/jpeg;base64," . base64_encode($row['Image']) . "' class='imge'>
This works!Thanks for the support!
I'm creating a music player, where a song can be added to a playlist. However, if the song has already been added to the playlist, I want to hide that playlist for that particular song. I say that because you can have more than one playlist and one song can be in multiple playlists.
I have the following code:
<?php
$shuffle = $_GET["shuffle"];
$songsContent = $db->query("SELECT * FROM songs WHERE song_band='$band' && album_name='$album'");
$numSongsContent = $songsContent->num_rows;
$number = 0;
while($fetch_songs = mysqli_fetch_array($songsContent)) {
$number++;
$song_path = $fetch_songs["song_path"];
$song_title = $fetch_songs["song_title"];
$song_band = $fetch_songs["song_band"];
echo "<div class='songs' path='Music/$song_band/$song_path.mp3' number='$number' total='$numSongsContent'> $song_title <div id='song_playlist'> <i class='fa fa-plus' aria-hidden='true'></i>
Add to Playlist <div id='song_playlist_popup'>";
$getPlaylists = $db->query("SELECT * FROM playlists");
while($fetch_playlists = mysqli_fetch_array($getPlaylists)) {
$playlist_name = $fetch_playlists["playlist_name"];
This is the part of the code that I'm having difficulty with. In this part, this is where the playlists are outputted. I'm trying to control this output by a conditional statement. But, the data seems to completely ignore the condition:
$checkDuplicates = $db->query("SELECT * FROM playlist_songs WHERE playlist_name='$playlist_name' AND song_name='$song_title'");
$numDuplicates = $checkDuplicates->num_rows;
if($numDuplicates == 0) {
echo "<div id='playlist_name'> <span song='$song_title'> $playlist_name </span> </div>";
}
}
echo "<div id='playlist_name2'> <span id='playlist_input_errors'></span> <input id='playlist_input' placeholder='Create new Playlist' song='$song_title'> </div> </div> </div> </div>";
}
?>
How do I fix this query to only allow playlists that haven't been added to a song? Thanks...
I am trying to combine two things which are already know how to do, but can't figure out how to combine them. Here is what I want to achieve:
I have a database with locations and events. There are several events in each location. I will be using PHP to query the database and output the code needed to display search results. I want something similar to the below:
<div id="location">
<p>Location1</p>
<div id="event">Event1</div>
<div id="event">Event2</div>
<div id="event">Event3</div>
</div>
<div id="location">
<p>Location2</p>
<div id="event">Event4</div>
<div id="event">Event5</div>
<div id="event">Event6</div>
</div>
I know that I can use select distinct to get the unique value of each location, and know that I can use a normal select statement to get all the events, however how do add all the events inside the location div?
My current PHP looks like this:
$sql ="SELECT location, event from events";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)){
$location = $row['location'];
$event = $row['event'];
echo "<div id="location">
<p>$location</p>
<div id="event">$event</div>
</div>";
}
My current code adds duplicates of the same location with 1 unique event in each. Even if I use select distinct I get the same results. How do I group the events have have the same location?
I think you should write something like:
$sql ="SELECT location, event from events";
$res = mysql_query($sql) or die(mysql_error());
$prevlocation = "";
while($row = mysql_fetch_assoc($res))
{
$location = $row['location'];
$event = $row['event'];
if ( $prevlocation != "" ) // Close previous div if needed
{
echo "</div>";
}
if ( $location != $prevlocation )
{
echo "<div id='location'><p>$location</p>";
$prevlocation = $location;
}
else
{
echo "<div id='event'>$event</div>";
}
}
echo "</div>"; // Close last div
If you have join of two tables, let's assume that your query looks like this:
$sql ="SELECT * FROM events
JOIN locations ON
locations.id=events.loc_id";
And then, within one loop, get events and location arrays:
$res = mysql_query($sql) or die(mysql_error());
$locations = array();
$events= array();
while($row = mysql_fetch_assoc($res))
{
$location = $row['location'];
$event = $row['event'];
$loc_id=$row['loc_id'];
$id=$row['id'];
$events[]=$loc_id.'%'.$event;
if(!in_array($id.'%'.$location,$locations)) { // avoid duplicate entries
$locations[]=$id.'%'.$location;
}
}
And, another loop (+loop inside loop):
for($i=0;$i<count($locations);$i++) {
$location=explode('%',$locations[$i]);
echo "<div class='location'>\n
<p>$location[1]</p>\n";
for($j=0;$j<count($events);$j++) {
$event=explode('%',$events[$j]);
if($event[0]==$locations[$i][0]) {
echo "<div class='event'>".$event[1]."</div>";
}
}
echo "</div>";
}
Not so elegant, but it is working, and produces valid HTML. :)
First, i wanted to make two associative arrays, and to compare keys, but i couldn't, because i couldn't convert ID keys to strings, so i made it with explode (% is separator between key and value).
I'm not getting it done, I need to put a class on the output of the function.
Can you help, when the menu item is active there has to be put a class to the link.
This is the code:
function loadMenu()
{
$result = mysql_query(" SELECT * FROM cms_page WHERE site_id =2 AND page_inmenu =1 AND page_active =1");
if ($DEBUG)
echo "<pre>$result</pre>";
while($row = mysql_fetch_array($result))
{ $last_parent = $row['page_name'];
echo "<a href={$row['link_name']}>{$row['page_name']}</a>";
}
}
function loadMenu()
{
$result = mysql_query(" SELECT * FROM cms_page WHERE site_id =2 AND page_inmenu =1 AND page_active =1");
if ($DEBUG)
echo "<pre>$result</pre>";
while($row = mysql_fetch_array($result))
{
$last_parent = $row['page_name'];
$class = $row['active'] === true ? ' class="active"' : '';
echo "<a href=\"{$row['link_name']}\"{$class}>{$row['page_name']}</a>";
}
}
Also your
I'm not 100% sure about what's your issue.
So, if you aren't displaying anything, probably a MySQL issue.
1- Reference for Mysql (Look at OZ_'s answer) : How to put mysql inside a php function?
2- As mentionned by Beiller, you'll need a way to see if the link is active.
Solution A : Might need some adjustment according $row['link_name'] value.
$class = ($row['link_name'] == $_SERVER['REQUEST_URI'].)?' class="active" ' : '';
Solution B : Simply stick to CSS - http://www.echoecho.com/csslinks.htm
.mylink:active{ background-color:pink; }
3- Href needs quotes on around the URL.
also, I'm not a big fan of {} inside strings.
echo "". $row['page_name'] ."";
Thanks beiller for the help..
This is the function now:
function loadMenu(){
$result = mysql_query(" SELECT * FROM cms_page WHERE site_id =2 AND page_inmenu =1 AND page_active =1");
if ($DEBUG)
echo "<pre>$result</pre>";
while($row = mysql_fetch_array($result))
{ $last_parent = $row['page_name'];
echo "<a href={$row['link_name']}>{$row['page_name']}</a>";
}}
This is the call:
<div id="header_menu">
<? loadMenu (); ?>
</div>
And mysql has the following tables:
page_id
page_name
link_name
site_id
order_id
page_menu
page_inmenu
page_leftmenu
page_text
page_active
I have this PHP code :
$query = "SELECT * FROM news WHERE news_active = 1 AND news_type = 1 ORDER BY id DESC";
$q2 = "SELECT * FROM st_photos WHERE id = 4 LIMIT 1";
$r2 = mysql_query($q2);
$row22 = mysql_fetch_array($r2);
$news_set = mysql_query($query);
$news_set2 = mysql_query($query);
if (mysql_num_rows($news_set) != 0) {
$r = mysql_fetch_array($news_set);
echo "<div id=\"d_coll\">
<div id=\"top_text\">$row22[img]</div>
<div id=\"d_image\"><img id=\"larg_p2\" src=\"photos/$r[news_image]\" width=\"320\" height=\"250\" border=\"0\"></div>
<div style=\"width:300px\"><div id=\"n_text2\">$r[news_part_en]</div>
</div>
</div>";
}
if (mysql_num_rows($news_set2) != 0) {
while ($news = mysql_fetch_array($news_set2)) {
echo "<div id=\"n_col\">
<div id=\"n_tittle\">$news[news_tittle_en] <img src=\"images/bu3.png\" border=\"0\" align=\"middle\"></div>
<div id=\"im\"><img onMouseOver=\"MM_swapImage('larg_p2','','photos/$news[news_image]','imgs[$news[id]]','','photos/$news[news_image]',1);up2('$news[news_part_en]')\" onMouseOut=\"MM_swapImgRestore()\" name=\"imgs[$news[id]]\" id=\"imgs[$news[id]]\" src=\"photos/$news[news_image]\" width=\"50\" height=\"50\"></div>
<div dir=\"ltr\" id=\"n_div\">$news[news_part_en] <div class=\"mo\">MORE</div></div>
</div>";
}
echo "<div align=\"right\" class=\"arr\"><img src=\"images/prev.png\"> <img src=\"images/next.png\"></div>";
}
There are 2 images at the end of the code (prev & next), I want to use them to do pagination but I don't want to view any numbers, only these 2 images.
How I can do that?
I think we can do that by using JQuery library, but I don't know how to use it.
Thanks in advance.
you can use one of many plugins, for example here .
you must just remoove thе numbers. you can, i believe;)
or you can write the script by yourself.
i'll give you only an idea. let's assume you have 30 rows( from DB).put them into <div> tags, and increase the id of div. the display proparty of first <div> you must set to '', and all others to none. and then, onclick on your buttons, you just change display proparty of div elements...