Not retrieving database fields dynamically inside a div using php - php

<div id="thumbnail-slider" >
<div class="inner" style="height:500px;">
<?php
$query = "select * from released_movies";
$queryr = $con->query($query);
while ($row = $queryr->fetch_assoc()) {
?>
<ul>
<li>
<img src="../AbaamAdmin/Released_Movies/<?php echo $row['rel_movies_pics']; ?>" width="900" height="500" class="thumb">
</li>
</ul>
<?php } ?>
</div> <!-- .inner-->
</div> <!-- .thumbnail-slider-->
The query is retrieving only 1 picture than the complete pictures given in the database. But if I remove the div , the query is executing fine. Thanks in advance.

Try this
<div id="thumbnail-slider">
<div class="inner" style="height:500px;">
<?php
$query="select * from released_movies";
$queryr=$con->query($query);
?>
<ul>
<?php
while($row=$queryr->fetch_assoc())
{
?>
<li>
<a href="single.php">
<img src="../AbaamAdmin/Released_Movies/<?php echo $row['rel_movies_pics'];?>" width="900" height="500" class="thumb">
</a>
</li>
<?php
}
?>
</ul>
</div> <!-- .inner-->
</div> <!-- .thumbnail-slider

You are starting and ending <ul> in while loop.
Which in incorrect, you should start it before while loop and end if after while loop.
Corrected code:
<?php
$query = "select * from released_movies";
$queryr = $con->query ( $query );
?>
<ul>
<?php
while ( $row = $queryr->fetch_assoc () ) {
?>
<li><a href="single.php">
<img src="../AbaamAdmin/Released_Movies/<?php echo $row['rel_movies_pics'];?>"
width="900" height="500" class="thumb"> </a></li>
<?php }?>
</ul>

Related

Retrieving a random unique row from the database table, 6 times, to populate and replace placeholder fields in HTML

I'm working on a fictional events website which should display various admin created events and I wish to generate unique pages for each of the featured events.
<div class="row">
<div class="4u 12u$(mobile)">
<article class="item">
<img src="../images/pic02.jpg" alt="" />
<header>
<h3>Event 1</h3>
</header>
</article>
<article class="item">
<img src="../images/pic03.jpg" alt="" />
<header>
<h3>Event 2</h3>
</header>
</article>
</div>
<div class="4u 12u$(mobile)">
<article class="item">
<img src="../images/pic04.jpg" alt="" />
<header>
<h3>Event 3</h3>
</header>
</article>
<article class="item">
<img src="../images/pic05.jpg" alt="" />
<header>
<h3>Event 4</h3>
</header>
</article>
</div>
<div class="4u$ 12u$(mobile)">
<article class="item">
<img src="../images/pic06.jpg" alt="" />
<header>
<h3>Event 5</h3>
</header>
</article>
<article class="item">
<img src="../images/pic07.jpg" alt="" />
<header>
<h3>Event 6</h3>
</header>
</article>
</div>
</div>
Instead of Event 1, Event 2 ... I want to have the actual eventName's from my events table in the database.
I've used the following code before to generate a table with the fields from the database:
<?php
$queryEvents = "SELECT * FROM events GROUP BY eventStart DESC";
$resultEvents = $mysqli->query($queryEvents);
?>
<table class="tableList" align="left">
<tr>
</tr>
<?php
while($rowEvents = $resultEvents->fetch_assoc()){
echo "<tr>";
echo "<td>{$rowEvents['eventName']}</td>";
echo "<td>View</t
d>";
echo "<td>Edit</td>";
echo "<td>Delete</td>";
echo "</tr>";
}
?>
</table>
This however only works with all the values in the database. How can I just retrieve 6 random values and each of them be unique to replace the Event 1 (etc) placeholders?
Your new query to retrieve randomly events:
$queryEvents = "SELECT * FROM events ORDER BY RAND() LIMIT 6";
$resultEvents = $mysqli->query($queryEvents);
Then, inside HTML, something like this:
<?php while( $rowEvents = $resultEvents->fetch_assoc() ): ?>
<div class="4u 12u$(mobile)">
<article class="item">
<img src="../images/pic04.jpg" alt="" />
<header>
<h3><?php echo $rowEvents['eventName'] ?></h3>
</header>
</article>
</div>
<?php endwhile; ?>
But you have 2 <article> in each <div>, so you can use a flag, like (i.e.) in this way:
<?php
$flag = True;
while( $rowEvents = $resultEvents->fetch_assoc() ): ?>
<?php if( $flag ): ?>
<div class="4u 12u$(mobile)">
<?php endif; ?>
<article class="item">
(...)
</article>
<?php if( $flag ): ?>
</div>
<?php
endif;
$flag = ! $flag;
?>
At the start, $flag is set to True, so at the first loop, the <div> and </div> will printed out; at the end of first loop, $flag is set to False, so at next loop the <div> will not printed out, at the end of second loop $flag is set to True, etc...
Your database query:
$queryEvents = $mysqli->query("SELECT * FROM events ORDER BY RAND() LIMIT 6");
$resultEvents = mysqli->fetch_assoc($queryEvents);
Your foreach loop to display data:
foreach ($resultEvents as $event) {
echo "<tr>";
echo "<td>{$event['eventName']}</td>";
echo "<td>View</t
d>";
echo "<td>Edit</td>";
echo "<td>Delete</td>";
echo "</tr>";
}

Display slide with two records at a time

I want to display slide with two records at a time. Now I am only getting only one record at a time in slider because I fetch data in while loop. My code is:
<ul class="slides">
<?php
...
while($eventData = mysql_fetch_array($eventSql))
{
?>
<li class="questions-slide-item">
<div class="query clearfix">
<div class="image fl">
<img src='<?php echo $eventData['image']; ?>' style="width:63px; height:61px;">
</div>
</div>
</li>
<?php } ?>
</ul>
You can add a counter and open/close the list item only every second iteration:
<ul class="slides">
<li class="questions-slide-item">
<?php
...
$count = 0;
while($eventData = mysql_fetch_array($eventSql))
{
if($count > 0 && $count % 2 == 0) {
?>
</li>
<li class="questions-slide-item">
<?php
}
?>
<div class="query clearfix">
<div class="image fl">
<img src='<?php echo $eventData['image']; ?>' style="width:63px; height:61px;">
</div>
</div>
<?php
$count++;
}
?>
</li>
</ul>
You can first take all the rows in one array and then use that array as per your needs.
See to following code to understand what I am trying to say.
<?php
...
$eventData= array();
while($row = mysql_fetch_assoc($eventSql))
{
$eventData [] = $row;
}
for($i=0;count($eventData);$i=$i+2)
{
?>
<li class="questions-slide-item">
<div class="query clearfix">
<div class="image fl">
<img src='<?php echo $eventData[$i]['image']; ?>' style="width:63px; height:61px;">
</div>
</div>
</li>
<li class="questions-slide-item">
<div class="query clearfix">
<div class="image fl">
<img src='<?php echo $eventData[$i+1]['image']; ?>' style="width:63px; height:61px;">
</div>
</div>
</li>
<?php } ?>

how to add product inside a php page when after executing the insert query

php file
<?php
require_once('conn.php');
$itemId= (\filter_input(\INPUT_GET,'itemId'));
$sql="select img from getdesserticecream where itemId='$itemId'";
$res=mysqli_query($dbhandle,$sql);
//$row= \mysqli_fetch_array($res);
//$subtitle=$row['subtitle'];
$row= mysqli_fetch_assoc($res);
//mysqli_close($dbhandle);
header("Content-type: image");
stripslashes($row['img']);
?>
<html>
<head>
<title>Customer menu card</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" type="text/css" href="dessert.css">
</head>
<body>
<form id='custdisp' name='custdisp' method='post' onload='return display()' enctype="multipart/form-data">
<script src="samcheck.js"></script>
<div id="top">
<?php session_start(); ?>
<div id="toplbl">Welcome <?php echo $_SESSION["name"]?></div>
<?php session_start(); ?>
<div id="toplbl1">This is Table Number <?php echo $_SESSION["tnumber"]?></div>
<input type="hidden" style="position: absolute;top:200px;" value="<?php echo $_SESSION["custno"]?>">
</div>
<div id="container">
<div id="header" >
<img id="headerimage" src="../samproject/contentimg.jpg" alt="image"/>
<label id="headerlbl1">Order </label>
<label id="headerlbl2">Eat</label>
<label id="headerlbl3">Pay !!!! </label>
</div>
<nav>
<ul>
<li><a href='#'>Starter</a>
<ul>
<li>Veg</li>
<li>NonVeg</li>
</ul>
</li>
<li>Main Course
<ul>
<li>Veg</li>
<li>NonVeg</li>
</ul>
</li>
<li>Dessert</li>
</ul>
</nav>
<div id="content">
<nav id="n1">
<ul id="ul1">
<li id="li1"> I want to see ---->
<ul id="ul2">
<li id="li2">Ice cream</li>
<li id="li3">Salad and Cakes</li>
<li id="li4">Milk shake</li>
</ul>
</ul>
</nav>
<div id="portfolio1">
<?php $sql1="select subtitle from dessert where itemId='oepd1007'";
$res1=mysqli_query($dbhandle,$sql1);
$row1= mysqli_fetch_array($res1);
$subtitle1=$row1['subtitle'];?> <?php echo $subtitle1 ?></div>
<div id="portfolio2">
<?php $sql2="select subtitle from dessert where itemId='oepd5148'";
$res2=mysqli_query($dbhandle,$sql2);
$row2= mysqli_fetch_array($res2);
$subtitle2=$row2['subtitle'];?> <?php echo $subtitle2 ?></div>
<div id="portfolio3">
<?php $sql3="select subtitle from dessert where itemId='oepd1004'";
$res3=mysqli_query($dbhandle,$sql3);
$row3= mysqli_fetch_array($res3);
$subtitle3=$row3['subtitle'];?> <?php echo $subtitle3 ?></div>
<div id="portfolio4">
<?php $sql4="select subtitle from dessert where itemId='oepd1008'";
$res4=mysqli_query($dbhandle,$sql4);
$row4= mysqli_fetch_array($res4);
$subtitle4=$row4['subtitle'];?> <?php echo $subtitle4 ?></div>
<div id="portfolio5">
<?php $sql5="select subtitle from dessert where itemId='oepd1522'";
$res5=mysqli_query($dbhandle,$sql5);
$row5= mysqli_fetch_array($res5);
$subtitle5=$row5['subtitle'];?> <?php echo $subtitle5 ?></div>
<div id="portfolio6">
<?php $sql6="select subtitle from dessert where itemId='oepd1002'";
$res6=mysqli_query($dbhandle,$sql6);
$row6= mysqli_fetch_array($res6);
$subtitle6=$row6['subtitle'];?> <?php echo $subtitle6 ?></div>
<div id="portfolio7">
<?php $sql7="select subtitle from dessert where itemId='oepd1003'";
$res7=mysqli_query($dbhandle,$sql7);
$row7= mysqli_fetch_array($res7);
$subtitle7=$row7['subtitle'];?> <?php echo $subtitle7 ?></div>
<div id="portfolio8">
<?php $sql8="select subtitle from dessert where itemId='oepd1001'";
$res8=mysqli_query($dbhandle,$sql8);
$row8= mysqli_fetch_array($res8);
$subtitle8=$row8['subtitle'];?> <?php echo $subtitle8 ?></div>
<div id="portfolio9">
<?php $sql9="select subtitle from dessert where itemId='oepd1005'";
$res9=mysqli_query($dbhandle,$sql9);
$row9= mysqli_fetch_array($res9);
$subtitle9=$row9['subtitle'];?> <?php echo $subtitle9 ?></div>
<div id="portfolio10">
<?php $sql10="select subtitle from dessert where itemId='oepd1010'";
$res10=mysqli_query($dbhandle,$sql10);
$row10= mysqli_fetch_array($res10);
$subtitle10=$row10['subtitle'];?> <?php echo $subtitle10 ?></div>
<div id="portfolio11">
<?php $sql11="select subtitle from dessert where itemId='oepd1011'";
$res11=mysqli_query($dbhandle,$sql11);
$row11= mysqli_fetch_array($res11);
$subtitle11=$row11['subtitle'];?> <?php echo $subtitle11 ?></div>
<div id="portfolio12">
<?php $sql12="select subtitle from dessert where itemId='oepd1006'";
$res12=mysqli_query($dbhandle,$sql12);
$row12= mysqli_fetch_array($res12);
$subtitle12=$row12['subtitle'];?> <?php echo $subtitle12 ?></div>
<ul id="grid">
<li>
<img src="getdesserticecreamimage.php?itemId=oepd1007" alt="image" id="img1"></li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd5148" alt="image" id="img2"></li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd1004" alt="image" id="img3"> </li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd1008" alt="image" id="img4" > </li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd1522" alt="image" id="img5" > <li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd1002" alt="image" id="img6" > </li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd1003" alt="image" id="img7"> </li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd1001" alt="image" id="img8"></li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd1005" alt="image" id="img9"> </li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd1010" alt="image" id="img10"> </li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd1011" alt="image" id="img11"> </li>
<li> <img src="getdesserticecreamimage.php?itemId=oepd1006" alt="image" id="img12"> </li>
</ul>
</div>
</div>
</form>
</body>
</html>
I have retrieved all the images from database and display in the php file, but as soon as I insert them into the database I need the images to be displayed in this file.
I'm doing this by giving the itemId, but I don't know how to give $itemId and retrieve all the images..
Try this, right after your menu closes, put in this code:
/* Your menu code */
</ul>
</nav>
<?php
$results = array();
$res = mysqli_query($dbhandle,'select subtitle,itemId from dessert');
# We are going to fetch all the subtitle and itemId from your table
while($row= mysqli_fetch_array($res)){
# And save it in an array
$results[] = array('subtitle'=>$row['subtitle'],'itemId'=>$row['itemId']);
}
# Now, let's display the portfolio div elements first
foreach($results as $key=>$val){
echo "<div id='portfolio{$key}'>".$val['subtitle']."</div>";
}
# And then the images:
echo "<ul id='grid'>";
#using the same array again but this time it will be the itemId
foreach($results as $key=>$val){
echo '<li><img src="getdesserticecreamimage.php?itemId='.$val['itemId'].'" alt="image" id="img'.$key.'"></li>';
}
?>
It's that simple. You need to learn to shorten your code by looping through the common pattern after all database connections are expensive - make use of more resources and time.

Make multiple PHP if statements more efficient

I am starting out with PHP for a WordPress and have written some code to put some social network icons in the footer. The way I have done it works, I'm simply calling the content of social network URL stored in the DB and if there is something the icon/link in the footer. But looks very inefficient to me, here is the code, does anyone know how to make it more efficient.
<?php
$social1 = of_get_option('fab_social_twitter_url');
$social2 = of_get_option('fab_social_facebook_url');
$social3 = of_get_option('fab_social_linkedin_url');
?>
<!-- divs for right social network icons column -->
<div class="eight columns">
<div class="social">
<ul>
<?php
if(!empty($social1)) {
?>
<li><img src="<?php echo of_get_option('fab_social_twitter_icon'); ?>" alt="Follow us on Twitter"></li>
<?php
}
?>
<?php
if(!empty($social2)) {
?>
<li><img src="<?php echo of_get_option('fab_social_facebook_icon'); ?>" alt="Follow us on Facebook"></li>
<?php
}
?>
<?php
if(!empty($social3)) {
?>
<li><img src="<?php echo of_get_option('fab_social_linkedin_icon'); ?>" alt="Follow us on Linkedin"></li>
<?php
}
?>
</ul>
</div>
</div>
Maybe:
<!-- divs for right social network icons column -->
<div class="eight columns">
<div class="social">
<ul>
<?php
foreach (array("twitter","facebook","linkedin") as $option)
($tmp=of_get_option('fab_social_'.$option.'_url')) && (print('<li><img src="'.of_get_option('fab_social_'.$option.'_icon').'" alt="Follow us on '.ucfirst($option).'"></li>'));
?>
</ul>
</div>
</div>
From a performance point of view, I don't think that there is anything to optimize in here, three separate cases tested individually
<?php
$social1 = of_get_option('fab_social_twitter_url');
$social2 = of_get_option('fab_social_facebook_url');
$social3 = of_get_option('fab_social_linkedin_url');
$icon1 = of_get_option('fab_social_twitter_icon');
$icon2 = of_get_option('fab_social_facebook_icon');
$icon3 = of_get_option('fab_social_linkedin_icon');
?>
<!-- divs for right social network icons column -->
<div class="eight columns">
<div class="social">
<ul>
<?php if(!empty($social1)) { ?>
<li>
<a href="<?php echo $social1; ?>">
<img src="<?php echo $icon1; ?>" alt="Follow us on Twitter">
</a>
</li>
<?php } ?>
<?php if(!empty($social2)) { ?>
<li>
<a href="<?php echo $social2; ?>">
<img src="<?php echo $icon2; ?>" alt="Follow us on Facebook">
</a>
</li>
<?php } ?>
<?php if(!empty($social3)) { ?>
<li>
<a href="<?php echo $social3; ?>">
<img src="<?php echo $icon3; ?>" alt="Follow us on Linkedin">
</a>
</li>
<?php } ?>
</ul>
</div>
</div>

how to slider gallery convert in php code with slider image and their thumbs image

I am using this slider gallery. i have problem while showing thumbs of images.
i used while loop only on UL and li tags, when i end while loop after thumbs. it will show all images in main image box.. can any one tell me how i can use this gallery in my php code
<div class="image-gallery">
<?php if($pic_count > 0){ ?>
<div id="wowslider-container1">
<div class="ws_images">
<?php while(!$rs_img_gallery->EOF) { ?>
<ul>
<li><img src="<?php echo MYSURL;?>img.gallery/<?php echo $rs_img_gallery->fields['image'] ?>" alt="" id="wows1_0"/></li>
</ul>
<?php $rs_img_gallery->MoveNext();
} // end while(!$rs_dc_gallery->EOF) ?>
</div>
<div class="ws_thumbs">
<div>
<img src="<?php echo MYSURL;?>img.gallery/<?php echo $rs_img_gallery->fields['image'] ?>" alt="" width="120" height="90"/>
</div>
</div>
</div>
<?php }else{
echo "<div class='notification information png_bg' style='width:90%;'>
<div>
".$infomsg['msg164']."
</div>
</div>";
}?>
</div>
You should take the <UL> out of the while loop
<div class="ws_images">
<ul>
<?php while(!$rs_img_gallery->EOF) { ?>
<li>
<img src="<?php echo MYSURL;?>img.gallery/<?php echo $rs_img_gallery->fields['image'] ?>" alt="" id="wows1_0"/>
</li>
<?php
$rs_img_gallery->MoveNext();
} // end while(!$rs_dc_gallery->EOF)
?>
</ul>
</div>

Categories