I’ve created a PHP file that creates dropdown list from a MySQL table. When selected I want each entry in the dropdown list, to change an image shown on the page. I can display the last image in the dropdown list, but cant get it to change thereafter.
Do I need to do displaying image part with javascript??
Any help greatly received (with code sample if possible)
Regards,
Iain
<script language=”Javascript”>
function setImage(select){
var image = document.getElementsByName("image-swap")[0];
image.src = select.options[select.selectedIndex].value;
}
</script>
<?php
$servername = "localhost";
$username = "xxxx";
$password = "xxxxxx";
$dbname = "xxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Select all data:
echo "All Tartans:<br><br>";
$sql = "SELECT ID, TartanID, Clan, Variation, Manufacturer, Supplement, SupplementVAT, ImagePath FROM Tartan";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row into dropdown list
?>
<select name="Tartan" id="Tartan" onchange="setImage(this);")
<?php
while($row = $result->fetch_assoc())
{
echo "ID: " . $row["ID"]. "<br>" .
"TartanID: " . $row["TartanID"]. "<br>" .
"Clan: " . $row["Clan"]. "<br>" .
"Variation: " . $row["Variation"]. "<br>" .
"Manufacturer: " . $row["Manufacturer"]. "<br>" .
"Supplement: " . $row["Supplement"]. "<br>" .
"SupplementVAT: " . $row["SupplementVAT"]. "<br>" .
"ImagePath: " . $row["ImagePath"]. "<br>";
$clan = $row["Clan"];
$img_path = $row["ImagePath"];
echo "<option value='$Clan' >$clan</option>";
}
?>
</select>
<!-- Display Image here -->
<div class="img-block">
<img src="<?php echo $img_path; ?>"
name="image-swap"
alt="<?php echo $clan; ?>"
title="<?php echo $clan; ?>"
width="200"
height="200" class="img-responsive" />
</div>
<?php
}
else
{
echo "0 results";
}
echo "<br>";
$conn->close();
?>
Related
I have this PHP code that SELECTS up to 5 data from the mySQL database
<?php
$servername = "localhost";
$username = "kjikk";
$password = "kjkkk";
$dbname = "ujkhjkjl";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT content_url, id FROM posts LIMIT 5";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<img src='http://cdn.bithumor.co/uploads/" . $row["content_url"]. "' width='50%' height='25%'> - Name: " . $row["firstname"]. " " . $row["lastname"]. " ";
}
} else {
echo "0 results";
}
$conn->close();
?>
When the 5 posts are echoed, the end up going vertically instead of next to each other and breaking (br tag)after two images are next to each other
Here's what it looks like: http://s9.postimg.org/vao63thr3/Screen_Shot_2015_05_25_at_11_16_03_AM.png
I want two images to be next to each other at the same time before they break to the next two images.
How do I do this?
You could create a div container, and for each image with name, you can wrap them into a div of 50% width from parent with a float: left;
HTML Structure:
<div class="container">
<div class="half"><img src="..."> Name</div>
<div class="half"><img src="..."> Name</div>
</div>
PHP:
if ($result->num_rows > 0) {
// output data of each row
<div class="parent">
while($row = $result->fetch_assoc()) {
echo "<div class='half'><img src='http://cdn.bithumor.co/uploads/" . $row["content_url"]. "'> - Name: " . $row["firstname"]. " " . $row["lastname"]. "</div>";
}
echo "</div>";
}
else {
echo "0 results";
}
CSS:
.parent {
width: 1000px; //your desired width
}
.parent .half {
float: left;
width: 50%;
}
This way you will ensure always that it's going to be 2 images per row.
Remind: As jeroen said to you, before doing the logic, first try to do the structure the page will have, with it's HTML and CSS only. After that, create the logic and put the structure you will need to it.
Add float:left;
while($row = $result->fetch_assoc()) {
echo "<div style='float:left;margin-right:5px;'><img src='http://cdn.bithumor.co/uploads/" . $row["content_url"]. "'> - Name: " . $row["firstname"]. " " . $row["lastname"]. "</div>";
}
It is showing that connection has been created. But I don't know what is the problem in query. After that query I tried to echo something and it is visible there in browser.
<html>
<head>
</head>
<body>
<?php
$servername = "localhost:8888";
$username = "root";
$password = "";
$dbname = "employees";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else {
echo "Connected successfully";
}
$res = mysqli_query($conn,"select * from dept_manager");
echo"dept_manager";
while($row=mysqli_fetch_assoc($res))
{
?>
<table>
<tr>
<td><?php echo $row['emp_no'] ?></td>
<td><?php echo $row['dept_no']?></td>
<td><?php echo $row['from_date'] ?></td>
<td><?php echo $row['to_date'] ?></td>
</tr>
</table>
<?php
}
?>
</body>
</html>
$sql = "select * from dept_manager";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
// your code here
}
Try with this..
Try this in the place of while loop:
<?php
$res = mysqli_query($conn,"select * from dept_manager");
echo"dept_manager";
echo "<table>";
while($row=mysqli_fetch_assoc($res))
{
echo "<tr><td>" . $row['emp_no'] . "</td><td>" . $row['dept_no'] . "</td><td>" . $row['from_date'] . "</td><td>" . $row['to_date'] . "</td></tr>";
}
echo "</table>";
?>
You have mentioned servername:localhost:8888 just check once remove
8888 and try.
I have tried with your code in my local system,same code working fine.
Just i have entered my database name and table name,it's working fine.
So you just follow my example and test once.
COde:-
<html>
<head>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydashboard";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else{
echo "Connected successfully";
}
$res = mysqli_query($conn,"select * from content_values");
while($row=mysqli_fetch_assoc($res))
{
?>
<table>
<tr>
<td><?php echo $row['name'] ?></td>
</tr>
</table>
<?php
}
?>
</body>
</html>
Output:-
Connected successfully
Name:
Test QA AndroidSD
image sampl
GMAIL
test PDF
Nat Geo Video
I'm quite new to php and sql and was wondering how i style the data correctly, so that it is displayed in a table rather than block text (see picture to see what i mean)
This is my php code that i have used.
$dbhandle = mysql_connect($servername, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("isongs",$dbhandle)
or die("Could not select examples");
$result = mysql_query("SELECT tbl_artists.artistname, tbl_songs.songtitle, tbl_songs.yearrelease, tbl_artists.genre, tbl_songs.price
FROM tbl_artists INNER JOIN tbl_songs ON tbl_artists.artistID = tbl_songs.artistID;");
if(!$result) { die(mysql_error()); }
//fetch the data from the database
while ($row = mysql_fetch_array($result)) {
//echo "Song:".$row{'songtitle'}." Year Released:".$row{'yearrelease'}."Price:".$row{'price'}."<br>";*/
echo "Artist:". $row[0]. " Song:". $row[1]. " Year Released:". $row[2] . " Genre:". $row[3] . " Price:". $row[4] . "<br>"; //display the results
}
//close the connection
mysql_close($dbhandle);
?>
Thank you.
PHP can't style your data. All it can do is retrieve the data from your database and put it on your webpage. What you need to do is style that data using HTML and CSS.
For example:
<div>
<p>Artist: <?php echo $row[0]; ?></p>
<p>Song: <?php echo $row[1]; ?></p>
<p>Year Released: <?php echo $row[2]; ?></p>
<p>Genre: <?php echo $row[3]; ?></p>
<p>Price: <?php echo $row[4]; ?></p>
</div>
<?php
$servername = "localhost";
$username = "root";
$password = "admin";
$dbname = "sbsuite";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * from sys_frm WHERE ParamType='Title';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["Data"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?
This is code that give me two results..
Client
Server
I want to insert them as link to another php page with image, depends on what we click on its goes to another php page...
Thanks for help!
This is a simple HTML. Replace the client/server php and jpg to what you want.
<img src="client.jpg" alt="Client" /><br />
<img src="server.jpg" alt="Server" />
With PHP
<?php
while ($row = $result->fetch_assoc()) {
?>
<img src="<?php echo $row["Data"]; ?>.jpg" alt="<?php echo $row["Data"]; ?>" /><br />
<?php
}
I seem to be having a problem inserting a <div> element in the correct area. I want to push a button, in this case "more info" and display more information under the first div area using jquery.
example:
<div id="Main-info">
<div id="more-info">
</div>
</div>
The problem I'm having is the "Main-info" area is calling a php script and grabbing info from the database and displaying it in a table. The second php file I have loads in the "more-info" div so when I push the more info button the second php files goes under the entire first div but i want it to go under the specific information that I click.
A better example of what I am trying to accomplish is like the wefollow.com site, where you push the more info button and more info display right under the info you click. In my case the additional info goes under the entire table at the buttom.
Here is the code:
index.php:
<?php
include("buy.functions.php");
?>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="listing.js"></script>
<div id="article-area">
<h1>Welcome</h1>
<div id="output-listings">
<div id="more-information">
</div>
<?php outputListingsTable(); ?>
</div><!--end output-listings-->
buy.functions.php:
<?php
function outputListingsTable()
{
$mysql = new mysqli('localhost', 'root', 'root', 'ajax_demo') or die('you\'re dead');
$result = $mysql->query("SELECT * FROM explore") or die($mysql->error);
if($result)
{
echo "<table> \n";
while($row = $result->fetch_object())
{
$id = $row->id;
$siteName = $row->site_name;
$siteDescription = $row->site_description;
$siteURL = $row->site_url;
$sitePrice = $row->site_price;
echo "<div id=\"" . $id . "\"> \n";
echo " <tr> \n";
echo " <td>" . $siteName . "</td> \n";
echo " <td>" . $siteURL . "</td> \n";
echo " <td><a id=\"" . $id . "\" class=\"more-info\" href=\"#\">More info</a></td> \n";
echo " </tr> \n";
echo "</div> \n";
}
echo "</table> \n";
}
}
?>
getinfo.php:
<?php
function outputDescriptionTable($id)
{
$mysql = new mysqli('localhost', 'root', 'root', 'ajax_demo') or die('you\'re dead');
$result = $mysql->query("SELECT * FROM explore WHERE id=" . $id) or die($mysql->error);
if($result)
{
echo "<table> \n";
while($row = $result->fetch_object())
{
$siteName = $row->site_name;
$siteDescription = $row->site_description;
$siteURL = $row->site_url;
$sitePrice = $row->site_price;
echo "<div id=\"more-information\"> \n";
echo " <tr> \n";
echo " <td>" . $siteDescription . "</td> \n";
echo " <td>" . $sitePrice . "</td> \n";
echo " </tr> \n";
echo "</div> \n";
}
echo "</table> \n";
}
}
?>
<?php $id = $_GET['id']; ?>
<?php outputDescriptionTable("$id"); ?>
listing.js:
$(function(){
$("tr").hover(function(){
$(this).addClass("hover");
}, function() {
$(this).removeClass("hover");
});
$('.more-info').click(function() {
$('#more-information').show().load('getinfo.php?id=' + $(this).attr('id'));
return false;
});
});
jQuery's closest should solve your problem. Something like:
$('.more-info').click(function(e) {
$(e.target).closest('.more-information').show().load('getinfo.php?id=' + $(this).attr('id'));
return false;
});
From the doc:
Closest works by first looking at the
current element to see if it matches
the specified expression, if so it
just returns the element itself. If it
doesn't match then it will continue to
traverse up the document, parent by
parent, until an element is found that
matches the specified expression. If
no matching element is found then none
will be returned.
Also, your 'more-information' divs should have the class more-information, as opposed to the ID, as IDs should be unique, else strange things might happen.