issues with php if statement - php

ok so i am pretty new to php. i am making a site listing different attractions, i have a details page where i display information for each attraction stored in a mysql database. i'm trying to write an if statement so an icon will show if the relevant attraction has for example disabled facilities and so on. I have a tables attractions, type, facilities and faciliteslink in the database.
In the facilitieslink i have the columns:
AttractionID
FacilityID
in the Facility table i have:
FacilityID
FalilityName
in the attractions table i have:
AttractionID
name
summary ect...
also here is the if statement and queries i was trying to make
$myQuery = "SELECT Attraction.*, Type.TypeName ";
$myQuery .= "FROM Attraction ";
$myQuery .= "INNER JOIN Type ON Attraction.Type = Type.TypeID ";
$myQuery .= "WHERE AttractionID=" .$_GET['ID'];
//run query
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($result));
?>
<?php
$myQuery .= "INNER JOIN Type ON Attraction.AttractionID = facilitieslink.FacilityID ";
?>
<?php
//display attractions row by row
while($row = mysqli_fetch_array($result))
{
echo '<div class=" sixteen columns" id="attraction-details">';
echo '<h3>Attractions/<a href="details.php?ID=' . $row['AttractionID'] . '">' . $row['Name'] . '</a></h3>';
echo ' <div class="eight columns" id="big-a-image1">';
echo ' <img src="'. $row['ImageUrl'] . '"/>';
echo ' </div>';
echo ' <div class="eight columns" id="big-a-image2">';
echo ' <img src="'. $row['ImageUrl2'] . '"/>';
echo ' </div>';
echo ' <div class="eight columns" id="big-a-image3">';
echo ' <img src="'. $row['ImageUrl3'] . '"/>';
echo ' </div>';
echo ' <div class="eight columns" id="big-a-image4">';
echo ' <img src="'. $row['ImageUrl4'] . '"/>';
echo ' </div>';
echo' <div class="two columns" id="m-thumb1">';
echo' <img src="'. $row['ImageUrl'] . '"/>';
echo' </div>';
echo' <div class="two columns" id="m-thumb2">';
echo' <img src="'. $row['ImageUrl2'] . '"/>';
echo' </div>';
echo' <div class="two columns" id="m-thumb3">';
echo' <img src="'. $row['ImageUrl3'] . '"/>';
echo' </div>';
echo' <div class="two columns" id="m-thumb4">';
echo' <img src="'. $row['ImageUrl4'] . '"/>';
echo' </div>';
echo ' <div class="eight columns" id="a-description">';
echo' <h4> Attraction Description </h4>';
echo ' <p class="para"> ' . $row['Description'] . ' </p></a>';
echo ' </div>';
echo' <div class="two columns" id="thumb1">';
echo' <img src="'. $row['ImageUrl'] . '"/>';
echo' </div>';
echo' <div class="two columns" id="thumb2">';
echo' <img src="'. $row['ImageUrl2'] . '"/>';
echo' </div>';
echo' <div class="two columns" id="thumb3">';
echo' <img src="'. $row['ImageUrl3'] . '"/>';
echo' </div>';
echo' <div class="two columns" id="thumb4">';
echo' <img src="'. $row['ImageUrl4'] . '"/>';
echo' </div>';
// everything else works apart from this
$id = $row["AttractionID"];
if ($row['FacilityID'] == 1){
echo' <img src="'. $row['icon'] . '"/>';
}
else
echo'</div>';
echo' <div class="eight columns" id="directions">';
echo' <h3>Directions</h3>';
echo' <p class="para"> ' . $row['Address'] . ' </p>';
echo' </div>';
echo' <div class="eight columns" id="video">';
echo' <h3>Video</h3>';
echo' ' . $row['VideoEmbed'] . ' ';
echo' </div>';
echo' <div class="eight columns" id="contact">';
echo' <h3 id="con"> Contact </h3>';
echo' <p class="para"> ' . $row['Long'] . ' </p>';
echo' </div>';
echo' <div class="eight columns" id="opening-times">';
echo' <h3 id="open"> Opening times </h3>';
echo ' <p class="para"> ' . $row['OpeningHours'] . ' </p>';
echo' </div>';
}
?>

Regarding your main question:
"would i store the icon in this table or 1 of the others?"
Which table is "this table"? You already mentioned 3 Tables now (Attraction, Facility and Type)
What shall this Icon show? The answer to this question can tell you, where the data belongs to: Types or attraction. Can every attraction have another icon? or does the Type tell you, which icon you have to choose?
You try to join the Type by FacilityID and another time by TypeID. Are both unique in your table Type? Otherwise you're going to get multiple rows for the same Attraction. Due to your Edit i now know you want to join a third table: Facility. You have to write INNER JOIN facilitylink ON Attraction.FacilityId = facilitylink.FacilityID INNER JOIN Facility ON facilityLink.FacilityId = Facility.FacilityId to solve your problem with a join. But beware that you may get above mentioned multiple rows of attraction. Furthermore, due to the properties of an inner join, you will loose information on attraction which don't have a facility. Maybe you should switch the first INNER JOIN into a LEFT JOIN, what will produce NULL assignments to unknown Facilities. Don't forget to add the Information you additionally need (Icon, FacilityId) to the SELECT-Part. I suggest you to search further information on how to write select-statements and what effect which Statement parts will have.
The Syntax of your SQL-Statement is messed up because you append (PHP-Operator .=) a Join-Part after the Where-part. You don't execute the manipulated query, is the Manipulation needed in this case? Change the position of appending the Join-Part before the Query-Command and adjust the Order to get a correct SELECT-Statement.
You're appending a user generated input without proper escaping to the query (.$_GET['ID']). This is a very common beginners failure. Please Take a look into the topic "SQL Injection" and prevent this by using "prepared statements"
Your Appending the content of $row['icon'] directly into the generated HTML. Is the Content given by the users or do you generate/declare it? In the first case you should escape it with htmlspecialchars($row['icon'])
you're not starting the php file properly. I think you cut of some code, but just in case: all php Code need to be behind a <?php. the ?>will command the interpreter to stop interpreting php-code at all. A new <?php will start the interpreter again.
<p> HTML-Code here
<?php
echo "PHP-Code here";
echo "more PHP-Code here";
echo "much more PHP-Code here";
?> </p><p>some more html code </p><?php echo "and some PHP-Code again";
echo "but this time until the Ent of the File!";
This also means, that you have some redundant code in your example:
?>
<?php
Hopefully i have directed you in the right direction.
EDIT: Edited some of the Sentences above due to your Edit.

You should check if this query is good because i'm not sure about it.
The code whould go like this... Assuming the table is "attraction"
// Define query
$query = "SELECT * FROM `facilitieslinks` WHERE `AttractionID` = '".$_GET['ID']."'";
// Create connection
$con=mysqli_connect("localhost","&&&&&YOURUSERNAME&&&&&","&&&&&YOURPASSWORD&&&&&","&&&&&YOURDATABASE&&&&&");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Execute the query to the DB
$result = mysqli_query($con,$query);
//Process data
while($row = mysqli_fetch_array($result))
{
$id = $row['AttractionID'];
if ($row['FacilityID']==1)
{
echo "<img src=\"".$row['icon'].\"">";
}
}
// Close the connection
mysqli_close($con);

Related

while looping w3-quarter in w3-row-padding

Here is the problem that while looping the php in while loop in w3-row-padding of w3 responsive layout . The layout breaks
Here is the source code
<?php
$r=0;
while($r<ceil($fetch_row_count/4))
{ ?>
<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">
<?php
while($row=mysqli_fetch_array($res))
{
?>
<div class="w3-quarter">
<img src="admin/uploads/<?php echo $row['image']; ?>" alt="noodles" style="width:50%">
<h3><?php echo $row['title']; ?></h3>
<p><?php echo $row['description']; ?> </p>
</div>
<?php
}
$r++;
}
?>
</div>
Thanks for reply and comments in advance
That bottom div was not being added for each of your padded containers.
The way the code is written you are adding a padded container and then adding your w3-quarter div for each of your result sets and then repeating that a bunch of times with what looks like to me the same set of data in each one.
What you are probably trying to accomplish is just making one padded div and then echo out your result set with the w3-quarter divs inside of it.
Here is your original way with the bottom div corrected:
<?php
$r=0;
while($r<ceil($fetch_row_count/4)) {
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
}
$r++;
echo
'</div>';
}
?>
Here is the way I think you are trying to do it: (Just guessing)
<?php
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
$r = 0;
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
$r++;
//I would not actually try to control how many results you add like this.
//I would get the results from a better formatted query.
if($r < ceil($fetch_row_count/4)){
break;
}
}
echo
'</div>';
?>

How can I fetch all the array of images from folder using explode function?

I am able to fetch only first image of every row of database.How can I fetch all the images using prepared statement and explode function.
<form method="POST" enctype="multipart/form-data">
<?php
$sql="select property_img from img_tbl";
$q=$conn->query($sql);
if($q->num_rows>0)
{
while($r1=$q->fetch_assoc())
{
$property_img1=$r1['property_img'];
$property_img2=explode(' ',$property_img1);
echo '
<div id="shade">
<div class="col-sm-12 " id="color">
<div class="col-sm-4 form-group">
<div class="row fppadd"> ';
?>
<?php
foreach($property_img2 as $data){
echo '<img class="img-zoom" src="uploads/' . $data . '" width="200" height="150" alt="not fetched"> ';
}
?>
<br></a></span><br>
</div>
</div>
</div>
</div> <?php
}
}?>
<br>
</form>
uploads folder structure of images
Images are saved in database in this format.I want to fetch these images
Try looping over the array after exploding by iterating array index :
for($i=0; $i<count($property_img2); $i++){
echo '<img class="img-zoom" src="uploads/' . $property_img2[$i] . '" width="200" height="150" alt="not fetched"> ';
}
However, I agree with the comments that your design is poor, and you might consider changing it to one-to-many relationship.

Display MySQL DB results in two different slides with RoyalSlider

I am using the RoyalSlider plugin (dimsemenov.com/plugins/royal-slider) to display the first 6 items from a MySQL database in the slider using PHP. I am using the LIMIT function to restrict each slide to 3 items so that the first slide displays items 1-3 from the DB and the second slide shows items 4-6. This works but it uses two separate SELECT / LIMIT queries which adds to page load time. Is it possible to merge the two separate LIMIT queries into one query?
The code I have so far is:
HTML / PHP
<div class="royalSlider rsDefault">
<div class="rsContent">
<ul>
<?php
$qry = mysql_query("SELECT * FROM properties LIMIT 0,3");
while($property = mysql_fetch_array($qry)) {
echo '<li><a href="/property.php?id='.$property['property_id'].'">
<img class="img-main" src="/files/'.$property['property_id'].'-1.jpg" title="" alt=""/>
<h2>'.$property['property_name'].'</h2>
<h3>'.$property['property_location'].'</h3>
</a>
</li>';
}
?>
</ul></div>
<div class="rsContent">
<ul>
<?php
$qry = mysql_query("SELECT * FROM properties LIMIT 3,3");
while($property = mysql_fetch_array($qry)) {
echo '<li><a href="/property.php?id='.$property['property_id'].'">
<img class="img-main" src="/files/'.$property['property_id'].'-1.jpg" title="" alt=""/>
<h2>'.$property['property_name'].'</h2>
<h3>'.$property['property_location'].'</h3>
</a>
</li>';
}
?>
</ul></div></div>
Please, try this code:
<div class="royalSlider rsDefault">
<div class="rsContent">
<ul>
<?php
$qry = mysql_query("SELECT * FROM properties LIMIT 0,6");
$i=1;
while ($i<=3 && $property = mysql_fetch_array($qry)) {
$i++;
echo '<li><a href="/property.php?id=' . $property['property_id'] . '">
<img class="img-main" src="/files/' . $property['property_id'] . '-1.jpg" title="" alt=""/>
<h2>' . $property['property_name'] . '</h2>
<h3>' . $property['property_location'] . '</h3>
</a>
</li>';
}
?>
</ul>
</div>
<div class="rsContent">
<ul>
<?php
while ($property = mysql_fetch_array($qry)) {
echo '<li><a href="/property.php?id=' . $property['property_id'] . '">
<img class="img-main" src="/files/' . $property['property_id'] . '-1.jpg" title="" alt=""/>
<h2>' . $property['property_name'] . '</h2>
<h3>' . $property['property_location'] . '</h3>
</a>
</li>';
}
?>
</ul>
</div>
</div>

Loop within a Loop PHP

Ok, this is my first time using Stack so I apologize in advance for anything I do incorrectly.
The Situation:
I have a class project to rewrite HTML code in PHP. Here is a snippet of the HTML code.
<div class="col-small-6 col-med-6 col-lg-4 albumContainer">
<img src="_images/elephant_king_cover_240x240.png" alt="">
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">Elephant King</a></h2>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>Elephant King</li>
<li>Joy & Sorrow</li>
<li>Traverse</li>
<li>Tres Capos</li>
<li>Timepiece</li>
<li>Adventures in Sawyerland</li>
<li>Be Still</li>
<li>Overtime</li>
<li>Bongolo</li>
<li>Coronation</li>
<li>Anchor</li>
</ol>
<h3>available at:</h3>
iTunes
Amazon
United Interests
</div>
</div>
</div>
</div>
</div><!-- end albumContainer -->
The structure repeats with different content.
The Problem:
When I run my SQl/PHP I am getting all of the information I am asking for, but it is not rendering properly. I get all of my information over and over again for as many different song titles there are (the ordered list).
I want everything to run one time for each section and for the song titles to be the content for the accordion. Here is the SQL/PHP code I have been messing around with.
<?php
require('mysqli_connect_remote.php');
$q = "SELECT Album_Art, Concat(Title, ' ', Release_Date) AS Title, destination, direction, Songs.Name FROM Albums Inner JOIN Songs ON Albums.Album_id=Songs.Album_id ";
$result = mysqli_query($dbcon, $q);
if ($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo '<div class="col-small-6 col-med-6 col-lg-4 albumContainer">';
echo '<img src=' . $row['Album_Art'] . 'alt="">';
echo '<div class="panel-group" id="accordion">';
echo '<div class="panel panel-default">';
echo '<div class="panel-heading">';
echo '<h2 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href=' . $row['direction'] . '>' . $row['Title'] . '</a></h2></div><div id=' . $row['destination'] . ' class="panel-collapse collapse"><div class="panel-body">';
echo '<ol> <li>' . $row['Name'] . '</li> </ol> </div></div></div></div></div>';
}
// <h3>available at:</h3>
// <a href=' . ['Location'] . '>iTunes</a>
// <a href=' . ['Location2'] . '>Amazon</a>
// <a href=' . ['Location3'] . '>United Interests</a>
// ';}
mysqli_free_result ($result);
} else {
echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
echo '<p>' . mysqli_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
}
mysqli_close($dbcon);
?>
Any insights on how to make this work for me would be most appreciated.
First, if you're doing loops in loops, or if-statements in loops, or loops in if-statements, indent better to where its clear what's in what. Sounds trivial, but it makes all the difference in the world.
And I would suggest losing the Egyptian brackets. You know, the ones that look like someone walking like an Egyptian. That is, start putting opening brackets on a new line so the opening and closing brackets line up which makes it easy to see when a bracket is missing.
I have re-indented your code without changing anything:
if ($result)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo '<div class="col-small-6 col-med-6 col-lg-4 albumContainer">';
echo '<img src=' . $row['Album_Art'] . 'alt="">';
echo '<div class="panel-group" id="accordion">';
echo '<div class="panel panel-default">';
echo '<div class="panel-heading">';
echo '<h2 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href=' . $row['direction'] . '>' . $row['Title'] . '</a></h2></div><div id=' . $row['destination'] . ' class="panel-collapse collapse"><div class="panel-body">';
echo '<ol> <li>' . $row['Name'] . '</li> </ol> </div></div></div></div></div>';
}
/*<h3>available at:</h3>
<a href=' . ['Location'] . '>iTunes</a>
<a href=' . ['Location2'] . '>Amazon</a>
<a href=' . ['Location3'] . '>United Interests</a>
';}*/
mysqli_free_result ($result);
}
else
{
echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
echo '<p>' . mysqli_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
}
mysqli_close($dbcon);
?>
Now you can actually see what's going on. Initially when I re-indented the first time I missed one of your closing brackets that was hiding at the end of a long long line and thought you were missing the closing bracket of the loop.
And it seems the problem is actually with your SQL query. Throwing a distinct in there might solve it:
$q = "SELECT DISTINCT Album_Art, Concat(Title, ' ', Release_Date) AS Title, destination, direction, Songs.Name FROM Albums Inner JOIN Songs ON Albums.Album_id=Songs.Album_id ";

how to use html inside php while loop

I have user information coming from database on a profile page using a while loop. I want to be able to write stuff like this, $username's profile. I can't seem to figure it out. Here is my code.
<?php
//open database connection
include 'page-start.php';
include 'core/init.php';
?>
<?php
$myQuery = ("SELECT user_id, username, profile, city FROM `users` WHERE user_id = '" . mysql_real_escape_string($_GET['ID']) . "' ") or die(mysql_error());
//run query
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($result));
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
} ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/studentsupport/defines.php'; ?>
<?php include_once("head.php");
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
echo '<div class="sixteen columns" id="user-profile">';
echo'<h2 class="username"> ' . $row['username'] . ' </h2> ';//bob's Profile
echo'<p>' . $row['city'] . '</p>'; //city: london
echo '<div class="eight columns" id="user-profile-img">';
echo'<img src="'. $row['profile'] . '"/>';
echo '</div>';
echo '</div>';
}
?>
edit: sorry I didn't explain it very well.
I want to be able to have some information come from the database and some information just as standard html for example:
<p><?php echo $username; ?>'s profile </p>
<p>city: <?php echo $city; ?> </p>
During each loop in the while, $row['username'] will have a username.
If you want to be able to get all the usernames later, add the following:
$users[] = $row['username'];
Now, Everywhere in your script $users[0] will have the 1st username, $users[1] will have the second, etc.
If you are beginner than i wil say You should use easy method now it seems okay
like You make Whole page of profile like you want
like
then you can Take your data in Variables in while Loop like that
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
$profile = $row['profile'];
$name = $row['name'];//you can take as many variables as you want
$city = $row['city'];
}
then you can use these variables in html simply
<div class="sixteen columns" id="user-profile">
<h2 class="username"><?php echo $name; ?> </h2>
</div>
start like that next step like above will get easy to u :)
This may do it.
<?php include_once("head.php");
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{ ?>
<div class="sixteen columns" id="user-profile">
<h2 class="username"><?php echo $row['username']; ?></h2>
<p><?php echo $row['city']; ?></p>
<div class="eight columns" id="user-profile-img">
<img src="<?php echo $row['profile']; ?>"/>
</div>
</div>
<? } ?>

Categories