My script is supposed to display the first 10 images in the MySQL database and have the script hide the rest of the users images until the user clicks the link View All and have the rest of the images slide down when the user clicks the link.
My Question: So my question is that my images won't display when the user clicks the link View All and I was wondering how can I fix this problem so that all my users images are displayed when the user clicks the link?
PHP & MySQL code.
$multiple = FALSE;
$row_count = 0;
$dbc = mysqli_query($mysqli, "SELECT * FROM images WHERE images.user_id = '$user_id'");
if (!$dbc) {
print mysqli_error($mysqli);
} else {
echo '<div id="images">';
while ($row = mysqli_fetch_array($dbc)) {
if (($row_count % 5) == 0) {
echo '<ul>';
}
echo '<li><img src="/images/thumbs/' . $row['url'] . '" /></li>';
if (($row_count % 5) == 4) {
$multiple = TRUE;
echo '</ul>';
} else {
$multiple = FALSE;
}
$row_count++;
}
if ($multiple == FALSE) {
echo '</ul>';
}
if ($row_count == 5) {
echo '</div>';
echo '<div id="hidden">';
}
}
echo '</div>';
echo 'View All';
JQuery code.
$(document).ready(function(){
$("#hidden").hide();
$("#view_all").click(function(){
$("#hidden").slideDown();
});
});
Here is the HTML code
<div id="images">
<ul>
<li><img src="../images/image.png" /></li>
<li><img src="../images/image.png" /></li>
<li><img src="../images/image.png" /></li>
<li><img src="../images/image.png" /></li>
<li><img src="../images/image.png" /></li>
</ul>
<ul>
<li><img src="../images/image.png" /></li>
<li><img src="../images/image.png" /></li>
<li><img src="../images/image.png" /></li>
<li><img src="../images/image.png" /></li>
<li><img src="../images/image.png" /></li>
</ul>
</div><div id="hidden"></div>
View All
You will need some images in that #hidden div. You could simplify the code by using css to hide your #hidden div (#hidden { display: none; }
Use the following jQuery:
$(document).ready(function(){
$("#view_all").click(function(){
$("#hidden").slideToggle("slow");
return false;
});
});
Another try.
You 'hidden' div is outside of loop, so no image can be printed in it. It becomes clear if you use some indentations in the code:
while($row = mysqli_fetch_array($dbc)){
if(($row_count % 5) == 0){
echo '<ul>';
}
echo '<li><img src="/images/thumbs/' . $row['url'] . '" /></li>';
if(($row_count % 5) == 4) {
$multiple = TRUE;
echo "</ul>";
} else {
$multiple = FALSE;
}
$row_count++;
}
if($multiple == FALSE) {
echo "</ul>";
}
if($row_count == 5) {
echo '</div>';
echo '<div id="hidden">';
}
update try moving if($row_count == 5) {...} right after $row_count++;. (I didn't run it, but looks like a right thing to do).
Pardon me if i missed something, but it doesn't seem like you put anything into the "hidden" div. That would make sense if you want to dynamically fetch the images from the server, which i recommend for faster load times. However, there is no jQuery code to do this. You could simply do this:
// get-more-photos.php or something
$multiple = FALSE;
$row_count = 0;
$dbc = mysqli_query($mysqli,"SELECT *
FROM images
WHERE images.user_id = '$user_id'
LIMIT ".mysql_real_escape_string($GET['offset']).",".mysql_real_escape_string($GET['offset'])+10."");
if (!$dbc) {
print mysqli_error($mysqli);
} else {
while($row = mysqli_fetch_array($dbc)){
if(($row_count % 5) == 0){
echo '<ul>';
}
echo '<li><img src="/images/thumbs/' . $row['url'] . '" /></li>';
if(($row_count % 5) == 4) {
$multiple = TRUE;
echo "</ul>";
} else {
$multiple = FALSE;
}
$row_count++;
}
if($multiple == FALSE) {
echo "</ul>";
}
On the client, you would call this with something like:
$("#view_all").click(function(){
$("#hidden").html("Loading...").slideDown();
$.get('get-more-photos.php?offset=10', '', function(data) {
$("#hidden").html(data).slideDown();
});
});
All untested, of course, but it should work with minor modifications.
Oh, and the View all button in this case will only show ten more, which is more reasonable depending on the number of photos. However, it only shows 10 more the first time, fixing that is left as an exercise for the reader :).
EDIT:
If you want, you don't have to dynamically fetch the images from the server (although i recommend it). For example, you could do something like the following:
// display-images.php
echo "<div>";
include "get-more-photos.php?offset=0";
echo "</div>";
echo '<div id="hidden">';
include "get-more-photos.php?offset=10";
include "get-more-photos.php?offset=20";
include "get-more-photos.php?offset=30";
include "get-more-photos.php?offset=40";
include "get-more-photos.php?offset=50";
include "get-more-photos.php?offset=60";
include "get-more-photos.php?offset=70";
include "get-more-photos.php?offset=80";
include "get-more-photos.php?offset=90";
echo "</div>";
Of course, that will only get the first 100, and might generate a bunch of empty lists, but if you have more than 100 images, or are worried about the extra lists, i would just fetch them dynamically :).
Related
I am having a problem with trying to show different menu options based on UserLevel. I have a mysql database with a users table. The users table contains a UserLevel which will either be set to 0 or 1. But for some reason my php just isn't working. In fact, when I add the php to the menu, it then does not display ANYTHING on the site below the menu. Any advice would be much appreciated.
Code that starts session
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
?>
<?php include "mainNav.php"; ?>
<center>
<h2> Campaign Updates</h2>
</center>
<div id="campaignPostWrap">
<div id="campaignScrollBox">
<?php
$con=mysqli_connect("localhost","dorians","ds2953!b67P$","aldentec");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM campaigns ORDER BY postDate desc");
while($row = mysqli_fetch_array($result))
{
echo "<div id='campaignPostContainer'>";
echo "<ul class='campaignPostBox'>";
echo "<p class='postInfo'>";
echo "Posted on:";
echo "<li>" . $row['postDate'] . "</li>";
echo "</p>";
echo "<p class='postInfo'>";
echo "Posted by:";
echo "<li>" . $row['postName'] . "</li>";
echo "</p>";
echo "<li class='postEntry'>" . $row['postEntry'] . "</li>";
echo "</ul>";
echo "</div>";
echo "<hr>";
}
mysqli_close($con);
?>
</div>
<?php include "campaignPost.php"; ?>
</div>
<?php include "chat.php"; ?>
<?php
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
$checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
if(mysql_num_rows($checklogin) == 1)
{
$row = mysql_fetch_array($checklogin);
$email = $row['EmailAddress'];
$userlevel = $row['UserLevel'];
$_SESSION['Username'] = $username;
$_SESSION['EmailAddress'] = $email;
$_SESSION['LoggedIn'] = 1;
$_SESSION['UserLevel'] = $userlevel;
echo "<h1>Success</h1>";
echo "<p>We are now redirecting you to the member area. If you are not automatically redirected <a href='index.php'>Click here</a></p>";
header( "refresh:10;url=index.php" );
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please click here to try again.</p>";
}
}
else
{
?>
Menu code that isn't working
<?php session_start(); ?>
<?php
$userlevel = $_SESSION['UserLevel'];
if($userlevel == 0) {
echo "<ul class="mainNav">
<li> Create Character</li>
<li> Create Quest</li>
<li> View Characters</li>
<li> View Quests</li>
<li> Book List</li>
</ul>";
} elseif($userlevel == 1) {
echo "<li> DM Tools</li>";
}
?>
<?php include "greeter.php"; ?>
Your quotes are undoubtedly the problem here:
if($userlevel == 0) {
echo "<ul class="mainNav">
<li> Create Character</li>
<li> Create Quest</li>
<li> View Characters</li>
<li> View Quests</li>
<li> Book List</li>
</ul>";
} elseif($userlevel == 1) {
echo "<li> DM Tools</li>";
}
Notice the syntax highlighting above shows the issue in your string. See how it turns black when it gets to mainNav? That's because mainNav is no longer part of the string. That's a bad thing here.
Look at the first line of your echo:
echo "<ul class="mainNav">
You open a quote and then close it at class=". Now, it's trying to evaluate mainNav as a constant or some other language construct. On top of that, it doesn't know what to do with mainNav as you haven't provided any kind of operators.
Instead, you should do something like:
if($userlevel == 0) {
echo '<ul class="mainNav">
<li> Create Character</li>
<li> Create Quest</li>
<li> View Characters</li>
<li> View Quests</li>
<li> Book List</li>
</ul>';
} elseif($userlevel == 1) {
echo '<li> DM Tools</li>';
}
Alternatively, you could escape every location where there is a non-string-terminating quote like \".
Another option would be to use Heredoc syntax.
I'm using a jquery image slider that, display images 3 by 3. All the images are coming from database. This is my code:
<ul class="bxslider">
<?php
$data = [ my images array];
for($x = 0; $x < count($data); $x++) {
?>
<li>
<div class="service_images">
<?php
$y=0;
while($y < 3) {
if($data[$y] != '') {
$imgNew = $data[$y];
?>
<img src="<?php echo $imgNew;?>" alt="" />
<?php
}
$y++;
}
?>
</div>
</li>
<?php
}
?>
</ul>
And this code is displaying just 1 image, and it's repeating.
plz help me guys. thanks.
Here I got this working... displayed 3x3 and you can check source... each img src increments by one each time. so that will give you 3 X 3.
heres my code, just need to adapt it a little (remove the array I made and replace it with yours.
<?php
$datas = array(1,2,3,4,5,6,7,8,9);
$y=0;
echo "<li>";
foreach ($datas as $data)
{
if($data != "")
{
$y++;
echo "<img src=" . $data . " alt='' />";
if($y % 3 == 0)
{
if($y < count($datas))
{
echo"</li>";
echo "<li>";
}
else
{
echo"</li>";
}
}
}
}
?>
</ul>
if you need any help, please comment.
Try this,
<ul class="bxslider">
<?php
$data = [ my images array];
for($x = 0; $x < count($data); $x++) {
?>
<li>
<div class="service_images">
<?php
if($data[$x] != '') {
$imgNew = $data[$x];
?>
<img src="<?php echo $imgNew;?>" alt="" />
<?php
}
?>
</div>
</li>
<?php
}
?>
</ul>
I am attempting to style a list of results from a foreach into two columns, however I'm not too sure how to go about it without throwing off the PHP.
Currently, everything looks like below:
and I would like it to look so:
Currently, my foreach and CSS looks thus:
<p class="title">Reviews</p>
<section id="reviews">
<ul class="section_body">
<?php
foreach($reviews['reviews'] as $rv){
if ($rtmp++ < 10);
if ($rv['freshness'] == 'fresh') {
$image = "fresh";
}
else {
$image = "rotten";
}
echo '<img src="assets/images/' . $image . '.png" class="rating" title="Rotten" alt="Rotten" />';
echo '<li>' . $rv['quote'] . '</li>';
}
?>
</ul>
</section>
Thank you in advance to anyone who may be able to help! It will be very gratefully received.
I normally use css to do this kind of task to let the browser take care of stuffs
#reviews li
{
float:left;
width: 45%;
...
}
add those 2 lines to your css and let browser takes care of the arrangement. The idea is having each li take half of the container (50%). I made it 45% to take care of extra padding, margin and border
Also, your quote and image should be in the li
<li> <img src="..." /> <span>my quote here</span> </li>
Try below code
<section id="reviews">
<?php
$i=0;
foreach($reviews['reviews'] as $rv)
{
if($i++%2 == 0)
{
echo '<ul class="section_body">';
}
if ($rtmp++ < 10);
if ($rv['freshness'] == 'fresh') {
$image = "fresh";
}
else {
$image = "rotten";
}
$img = '<div><img src="assets/images/' . $image . '.png" class="rating" title="Rotten" alt="Rotten" /></div>';
echo '<li>' .$img . $rv['quote'] . '</li>';
if($i%2 == 0 || $i== count($reviews['reviews']))
{
echo '</ul>'
}
}
?>
</section>
You should put image and review text inside the li element.
I'm trying to populate mysql table inside ul li tags. I couldn't be success. I don't know how to use jquery to get mysql data.
Can you guys help me please?
I'm getting my result on getdata.php without any problem. However I couldn't success to show result inside main.php without refreshing page.
main.php
<form name="cnt" id="cnt" action="get_shipping_price.php" method="Post" >
<select id="c_list" name="country" style="width:160px;" onChange="this.form.submit()">
<?php
$countries = $database->getCountry();
foreach ($countries as $country) {
echo '<option value="' . $country['printable_name'] . '" ' . ($req_user_info['country'] == $country['numcode'] ? 'selected="selected"' : '') . '>' . $country['printable_name'] . '</option>';
}
?>
</select>
</form>
<!-- result list box -->
<div id="result_box">
<ul>
<li class="blue_heading">A</li>
<li class="blue_heading">O / E</li>
<li class="blue_heading">I / I</li>
<li class="lgray_lis"></li>
<li class="lgray_lis"></li>
<li class="lgray_lis"></li>
<li class="dgray_lis"></li>
<li class="dgray_lis"></li>
<li class="dgray_lis"></li>
</ul>
</div>
get_data.php
<?php
include("include/database.php");
if(isset($_POST['country']) )
{
$cnt_id = $_POST['country'];
$result = $database->select("Select * From zone Where DHL = '$cnt_id' ");
foreach ($result as $row) {
$row['zone'];
}
}
$result = $database->select("Select * From price_in_dis");
foreach ($result as $indis)
{
if ($row['zone'] == 1)
{
echo "<ul>";
echo "<li>" .'y'.$indis['A']. "</li>";
echo "</ul>";
}
if ($row['zone'] == 2)
{
echo "<ul>";
echo "<li>" .$dis['C']. "</li>" ;
echo "</ul>";
}
if ($row['zone'] == 3)
{
echo "<ul>";
echo "<li>" .$dis['D']. "</li>" ;
echo "</ul>";
}
}
?>
I see 3 options,
Use a form or something to submit the value.
Make the Li clickable with with link where you pass through the variable value.
Use Ajax and Jquery to make some functions on 'click submit this'. There is a lot of different ways to do this, you can create a hidden form for example. ( I recommended this one only if you add some security in both sides, client and server)
U'll need a php file anyway the different is how you pass the parameter.
I have this query
$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());
$row_people = mysql_fetch_assoc($people);
$totalRows_people = mysql_num_rows($people);
I can echo the results within a unordered list using a while loop like this
<ul>
<?php {do { ?>
<li><?php echo $row_people['name'];?></li>
<?php } while ($row_people = mysql_fetch_assoc($people));}?>
</ul>
But I can't used this as my html does not allow it.
<ul>
<li class="first">
Kate
<li>
<li class="second">
<img src="john.jpg" />John
<li>
<li class="third">
<span>Max</span>
<li>
</ul>
My question is how can echo the name that was retrieved from the database into the appropriate place within this html?
Thanks for your help.
Try this:
<?php
$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());
if(mysql_num_rows($people) > 0){
?>
<ul>
<?php
while ($row_people = mysql_fetch_assoc($people)){
?>
<li><?php echo htmlentities($row_people['name']);?></li>
<?php
}
?>
</ul>
<?php
}
?>
You'll just have to create a renderer for each "type" of user (assuming you have a type property on the user rows) or based on their attributes. For example, let's say you're going to have to filter based on the attributes:
<?php
function render_simple($person) {
return '' . $person['name'] . '';
}
function render_with_image($person) {
return '<img src="' . $person['image'] . '.jpg"/>' . $person['name'] . '';
}
function render_special($person) {
return '<span>' . $person['name'] . '</span>';
}
function render_person($person) {
if ($person['image']) {
return render_with_image($person);
}
if ($person['special']) {
return render_special($person);
}
return render_simple($person);
}
$i = 0;
while ($row_people = mysql_fetch_assoc($people)){ ?>
<li class="index<?php echo ++$i; ?>">
<?php echo render_person($person); ?>
</li>
<?php
}
?>
This should work, with the exception that instead of class names first, second, etc, you'll now have index1, index2, etc.