Using php code in html to list images - php

I'm using this code :
<?php
// Grab the data from our people table
$sql = "select * from people";
$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());
while ($row = mysql_fetch_assoc($result))
{
echo "<br><br><br><br>";
echo $row['fname'] . " " . $row['lname'] . "<br />";
echo "<img class=\"picture2\" src=\"images/" . $row['filename'] . "\" alt=\"\" /><br />";
echo "</p>";
}?>
to list some images that have been saved by name in the database ... but when I want to put them in a table (using bootstrap components), it doesn't show any images ... my code is :
<div class="row">
<div class="col-md-6">
<?
while ($row = mysql_fetch_assoc($result))
{
echo $row['fname'] . " " . $row['lname'] . "<br />";
echo "<img class=\"img-rounded\" src=\"images/" .$row['filename'] . "\" alt=\"\" /> <br />";
echo "</p>";
}?>
</div>
</div>
Do you have any clue what i've to do to list images in the table ?
and my second question is how can I list images in just two columns ? it's not important how many rows does it take to show images but i want it to show two images in a line (but in two separated cells) ...

Related

How to get a post in php where use sql like that :

How do i get the row id?
I'm doing a site of music, i need do the admin page,this part is when the admin gonna edit the albums. I need the id to specify what music gonna change the informations.
<?php
$db = mysqli_connect("localhost", "root", "", "datamusic");
$result = mysqli_query($db, "SELECT * FROM musics");
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img src='images/" . $row['Image'] . "' >";
echo "<p>" . $row['Band/Singer'] . "</p>";
echo "<form method='POST' action='editInformations.php'><p name=" . $row['id'] . ">ID = " . $row['id'] . "</p></form>";
echo "<a href='editInformations.php'><button type='submit'>Editar</button></a>";
echo "</div>";
}
?>
I try something like that, but doesn't works
//editInformations.php
$id =$_POST[$row['id']];
echo $id;
Get edit page GET funciton. You can put id for edit.
editInformations.php?id=".$row['id']."
Change code with this.
<?php
$db = mysqli_connect("localhost", "root", "", "datamusic");
$result = mysqli_query($db, "SELECT * FROM musics");
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img src='images/" . $row['Image'] . "' >";
echo "<p>" . $row['Band/Singer'] . "</p>";
echo "<form method='POST' action='editInformations.php'><p name=" . $row['id'] . ">ID = " . $row['id'] . "</p></form>";
echo "<a href='editInformations.php?id=".$row['id']."'><button type='submit'>Editar</button></a>";
echo "</div>";
}
?>
//editInformations.php
$id =$_GET['id'];
echo $id;

My dropdown list would not drop down

I tried to use php to create a dropdown list populated by a SELECT query but it would not dropdown. Here is my php script
<?php
require_once ('mysqli_connect.php');
#mysqli_select_db($dbc,"test");
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = #mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
// while($row = #mysqli_fetch_array($result))
// {
// echo "<p>$row[0] $row[1]</p>\n"; //this works
// }
echo "<td bgcolor=\"#E6E6E6\"";
echo "<strong>Category:</strong>";
echo "<select name=\"category\">";
while($row = #mysqli_fetch_array($result))
{
// echo "<p><option value='".$row[0]."'>".$row[1]."</option></p>\n";
echo "<option value='".$row[0]."'>".$row[1]."</option>";
}
mysqli_close($dbc);
echo "</select>";
echo "</td>";
?>
The query worked (the commented while loop outside the dropdown list displayed 15 records). But the dropdown list only showed one category_name and would not dropdown. Can someone help me figure out the problem? Thanks.
There are a few problems with your code.
You can either remove both echo "<td bgcolor=\"#E6E6E6\""; and echo "</td>";
which is the reason why your data is not showing.
Or, add the appropriate <table></table> tags.
Plus this echo "<td bgcolor=\"#E6E6E6\""; should be echo "<td bgcolor=\"#E6E6E6\">"; there was a missing > so that alone is breaking your <td>.
So in order to have the proper table syntax do:
Sidenote: I added . "\n" in order to get clean and well-aligned HTML source.
<?php
require_once ('mysqli_connect.php');
#mysqli_select_db($dbc,"test");
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = #mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
echo "<table>" . "\n";
echo "<tr>" . "\n";
echo "<td bgcolor=\"#E6E6E6\">" . "\n";
echo "<strong>Category:</strong>" . "\n";
echo "<select name=\"category\">" . "\n";
while($row= mysqli_fetch_array($result)){
echo "<option value='".$row[0]."'>".$row[1]."</option>" . "\n";
}
echo "</select>" . "\n";
echo "</td>" . "\n";
echo "</tr>" . "\n";
echo "</table>" . "\n";
mysqli_close($dbc);
?>
Edit: (Try this) since I don't know what your full code looks like.
<form id="form1" name="form1" method="post" action="form1_handler.php">
<?php
require_once ('mysqli_connect.php');
#mysqli_select_db($dbc,"test");
if(isset($_POST['submit'])){
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = #mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
echo "<table>" . "\n";
echo "<tr>" . "\n";
echo "<td bgcolor=\"#E6E6E6\">" . "\n";
echo "<strong>Category:</strong>" . "\n";
echo "<select name=\"category\">" . "\n";
while($row= mysqli_fetch_array($result)){
echo "<option value='".$row[0]."'>".$row[1]."</option>" . "\n";
}
echo "</select>" . "\n";
echo "</td>" . "\n";
echo "</tr>" . "\n";
echo "</table>" . "\n";
mysqli_close($dbc);
} // brace for if(isset($_POST['submit']))
?>
<input type="submit" name="submit" value="Submit">
</form>

Php - how to check whether a row exists and display different things based on the result?

I have an attraction page that displays some information from the database, and I've put a favorite button with each display. This is my code:
$villageId = $_GET['village'];
$sql = "SELECT `AttractionID`, `Name`, `Location`, `Description`, `Airport`, `imglink`,`imglink2`,`imglink3`,`imglink4`,`imglink5` FROM `attractions` WHERE `AttractionID`='$villageId'";
$result = mysql_query($sql, $link);
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo "<div class='attractions6'>";
echo "<div class='attractions5'>";
echo $row['Name'];
echo "<span title='Add to Favorites'>" . "<a href='favourite.php?village=" . $row['AttractionID'] . "'>" .
"<img src='img/fav.png'></a>";
echo "</span>" ;
echo "<br />" ;
echo "<br />";
echo "</div>";
echo $row['Description'];
echo "<br />" ;
echo "<br />";
echo " Location: ";
echo $row['Location'] ." ";
echo "<br />" ;
echo "<br />";
echo " Nearest Airport: ".$row['Airport'] ." ";
echo "<br />" ;
echo "<br />";
echo "</div>";
echo '<img src="'.$row['imglink'].' " height="200" width="550">';
echo "<br />";
echo '<img src="'.$row['imglink2'].' " height="180" width="180">';
echo "&nbsp";
echo '<img src="'.$row['imglink3'].' " height="180" width="180">';
echo "&nbsp";
echo '<img src="'.$row['imglink4'].' " height="180" width="180">';
echo "&nbsp";
echo '<img src="'.$row['imglink5'].' " height="180" width="180">';
echo " ";
//This is the favorite button. If the user clicks on it the script
//will redirect to the favourite.php page where the database will be
//checked for duplicates in the attractionID field and will save the favorite.
echo "<br />";
echo "<br />";
}
mysql_free_result($result);
?>
I have the following code which I'm using on other places of the website that can check whether the input already exists:
$sql="SELECT `ID`,`AttractionID` FROM `favourites`
WHERE `ID`='$ID' AND `AttractionID`='$villageId' ";
$result = mysqli_query($con,$sql);
if ($result->num_rows > 0) {
header('location:attraction.php?village='.$villageId);
}
On the attraction page how can I make it so that if the user already saved this entry to favorites - I will display picture1 and if they didn't - I will display picture two?
Thanks.

How do I filter a MySQL query with a select option which results in the right product

I'm working on an CMS which is based on an database of course.
The structure of the database is as follow (the language of this is Dutch):
id, artikelnummer_fabrikant, breedte, diepte, hoogte, extrainfo, afmeting, kleur, merk, serie, producttype, omschrijving, prijsfinal, levertijd
With the next piece of code I get all the information out of the database and place it in a table:
(Sorry that I dont use < and > to create the tags, this text editor sees it as tag and not as text and I have no idea how to make this work correct, anny tips on this will be welcome!)
function get_content() {
echo "<p>";
echo "<table>";
echo "<tr>";
echo "<td> id </td>";
echo "<td> artikelnummer frabrikant </td>"
and so on
echo "</tr>";
$sql = "SELECT * FROM products";
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) != 0) :
while ($row = mysql_fetch_assoc($res)) :
echo "<tr>";
echo "<td> . $row['id'] . </td>";
echo "<td> . $row['artikelnummer_fabrikant'] . </td>";
and so on
echo "</tr>";
endwhile;
echo "</table>";
echo "</p>";
endif;
Now I would like to build a filter on this with the tags select and option so that when I select the option brand(merk) 'windsor' it will show all products with the brand 'windsor' with all there information such as breedte, hoogte, diepte (width, heigt, depth).
I got a piece of code which gives me all the brands(merk) in an option menu:
function category_filter() {
echo "Merk: ";
echo "<select onchange=window.location = this.value>;";
$sql = "SELECT merk FROM products";
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) !=0 ) :
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) :
echo "<option> . $row['merk'] . </option>";
endwhile;
endif;
echo "</select>";
And of cource I have 1 for each item in the database.
What I want to do next is combine these 2 codes in some kind of way that when i select the brand(merk) 'Windsor' in the option menu brand(merk) it will show all products with the brand 'Windsor' + all the other information of that product.
I'm struggeling for a week trying to make this happend and I can't get it going like the way i want it to go :(.
How can i make this work?
Ok so i found out how to do this by writing the following code:
echo "<form class='merk' action='index.php'>";
echo "<select name=merk>";
echo "<option value=''>Kies een merk</option>";
while ($row_merk = mysqli_fetch_array($merk_query_selected)) :
$filter = $row_merk['merk'];
if($filter == $merk) :
echo "<option value=" . urlencode($filter) . " selected='selected'>" . $filter . "</option>";
else :
echo "<option value=" . urlencode($filter) . ">" . $filter . "</option>";
endif;
endwhile;
echo "</select>";
echo "<input class='input-merk' type='submit' value='Filter op merk'>";
echo "</form><br />";
$('form').each(function() {
var that = $(this);
$.post(that.attr('action'), that.serialize());
});</script>
Have it decoded:
$merk = urldecode($_GET["merk"]);
And then get the contents by running this while loop:
while ($row_merk = mysqli_fetch_array($merk_query_all)) :
echo $table_row;
echo $table_cell, $row_merk['id'] . $table_div_end;
echo $table_cell, $row_merk['artikelnummer_fabrikant'] . $table_div_end;
echo $table_cell, $row_merk['breedte'] . $table_div_end;
echo $table_cell, $row_merk['diepte'] . $table_div_end;
echo $table_cell, $row_merk['hoogte'] . $table_div_end;
echo $table_cell, $row_merk['extrainfo'] . $table_div_end;
echo $table_cell, $row_merk['afmeting'] . $table_div_end;
echo $table_cell, $row_merk['kleur'] . $table_div_end;
echo $table_cell, $row_merk['merk'] . $table_div_end;
echo $table_cell, $row_merk['serie'] . $table_div_end;
echo $table_cell, $row_merk['producttype'] . $table_div_end;
echo $table_cell, $row_merk['omschrijving'] . $table_div_end;
echo $table_cell, $row_merk['prijsfinal'] . $table_div_end;
echo $table_cellEnd, $row_merk['levertijd'] . $table_div_end;
echo $table_row_end;
endwhile;
Now what i wanna do is make it dynamic so that it can filter on multiple selections at the same time.
Doing my best, anny help would be welcome!
[EDIT] Found out how this works, but its still not dynamic. But this question has been awnsered, I will go and look on the web how to make this dynamic.

Add link to image when echoing in PHP

In my post form, I have a "show"-page, where I echo out everything in the database. I'm trying to add a link to the image-line. But when I refresh the page, it's just white.
<?php
$sql_connection = mysql_connect("localhost", "user", "root");
mysql_select_db("database_name", $sql_connection);
$result = mysql_query("SELECT * FROM content ORDER BY id DESC");
while ($row = mysql_fetch_array($result)) {
echo "<div>";
echo "<img src=\"http://placehold.it/80x80\"> ";
echo "<br />";
echo "<br />";
echo "<h3>" . $row["who"] . "</h3><br />";
echo "<h4>" . $row["what"] . "</h4><br />";
echo "<h4>" . $row["where"] . "</h4><br />";
echo "<h3>" . $row["contact"] . "</h3><br />";
echo "<img src=\"../img/share_facebook.png\"> ";
echo "</div>";
}
mysql_close($sql_connection);
?>
Someone who can explain why this happens?
echo "<img src=\"http://placehold.it/80x80\"> ";
you have to escape() ALL your " or change to:
echo '<img src="http://placehold.it/80x80">';

Categories