using dynamic data in a javascript method - php

I'm new at php and JavaScript so I hope you can help me!
below is a php code! it take data from my db and display my photos and photo's title. what I want to do is if you click on one of this photos and than a new php expe: photo.php opens and you can see the same photo alone not with the other ones.
<?php
$link = mysql_connect("localhost", "root","");
mysql_select_db("test", $link);
$query = "select * from post order by id DESC;";
$results = mysql_query($query, $link) or die ("you comand can't be executed!".mysql_error());
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
echo $row['title']. "<img src=/1/" . $row['location']. " width=580px > " . "<br /><br /><br />";
}
}
?> '

<?php
$link = mysql_connect("localhost", "root","");
mysql_select_db("test", $link);
$query = "select * from post order by id DESC;";
$results = mysql_query($query, $link) or die ("you comand can't be executed!".mysql_error());
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
//send photo_id (id of photo from your table) from url NOTE: I've put link here
echo $row['title']. "<a href='index.php?photo_id=".$row['id']."'><img src=/1/" . $row['location']. " width=580px ></a> " . "<br /><br /><br />";
}
if(isset($_GET['photo_id'])) {
//here you get only one id of photo now retrieve the data from database and display the image
}
}
?>
You will get the photo by the help of the url value here photo_id.

do this way you are getting results from database and you are
displaying them with tile and image your aim is to click on
image or image title go to a new page and display image alone
the image title as link in first page
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
<a href='new page url?id=<?php echo $row["id"] ?>'>
echo $row['title'];
echo "</a>"
}
}
where $row['id'] is database unique id if have no it is always preferred
to create a database with unique id and in new page get the uid
using $id=$_GET['id'] and
do as below
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
if($id==$roe['id']{
echo $row['title']. "<img src=/1/" . $row['location']. " width=580px > ";
}
}
}

Related

Trying to insert image from database onto news blog above each entry using PHP

I am creating a website and would like to allow an admin to add an image that is being pulled from a MySQL database and have it display above the news blog text that is being added as well. I can get it to display the images, as well as the text, but they are grouped together (images with images and text with text). How can I have my website display a the last image entered above the last text entry added?
<?php
$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC";
$result = mysqli_query($dbc, $sql);
?>
</BODY>
</HTML>
<div class="brown-container-fluid text-left">
<div class="text-home">
<h2><strong>Pine Lane News</strong></h2><br/>
<?php
if ($row = mysqli_fetch_array($result)) {
?>
<div style="text-align:center;">
<img style="max-width:300px; max-height:300px;"
src="imageView.php?image_id=<?php echo $row["imageId"]; ?>"/><br/>
</div>
<?php
}
$query = 'SELECT * FROM entries ORDER BY date_entered DESC';
if ($r = mysqli_query($dbc, $query)) { // Run the query.
// Retrieve and print every record:
while ($row = mysqli_fetch_array($r)) {
print
"<dl><dt><h3><strong>{$row['title']}</strong></h3></dt>
<dd>{$row['entry']}<br /><br />\n</dd></dl>";
}
} else { // Query didn't run.
print '<p style="color: red;">Could not retrieve the data because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
} // End of query IF.
mysqli_close($dbc); // Close the database connection.
?>
</div>
</div>
Something I came up with real quick, not tested.
<?php
while($row = mysqli_fetch_array($result)){
echo '<div style="text-align:center;">';
echo '<img style="max-width:300px; max-height:300px;"';
echo 'src="imageView.php?image_id="'.$row["imageId"].'"/><br/>';
echo '</div>';
$query = 'SELECT * FROM entries ORDER BY date_entered DESC LIMIT 1';
if ($r = mysqli_query($dbc, $query)) { // Run the query.
while ($row = mysqli_fetch_array($r))
{
print
"<dl><dt><h3><strong>{$row['title']}</strong></h3></dt>
<dd>{$row['entry']}<br /><br />\n</dd></dl>";
}
} else { // Query didn't run.
print '<p style="color: red;">Could not retrieve the data because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
} // End of query IF.
mysqli_close($dbc); // Close the database connection.
?>

Sending PHP variables to another page using Sessions:

I'm having trouble with a couple of pages: I want to list all my images from a database (urls stored) and then have the user click the image which takes them to another page which then gives the rest of the information from the database.
I'm using a Session variable to send the ID of the image which is then used on the other page to select information from the database before viewing. The problem I have is that it always seems to show the last entered detail on the database and not the actual information from the variable of the image clicked.
I'm not sure where this is going wrong, I've since added plenty of session_destroy() commands which I thought was the initial problem but have not solved the issue and appear only to clog my code.
Any help or direction would be greatly appreciated.
Here's my code:
Page 1 - Sending variable through Sessions
<?php
//code to access database
$query = "SELECT * FROM releases";
$result = mysql_query($query);
if(!$result) die ("Database access failed: " .mysql_error());
$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo '<img src="images/releases/' . $row[1] .'" id="' . $row [0] . '"/>';
}
$id = $row [0]; //"row 0" is the auto increment of the database id.
if (isset($_SESSION ['id'])){
session_destroy();
}
session_start();
$_SESSION ['id']= $id;
mysql_close($db_server);
?>
Page 2 - Receiving the variable and then using it to access the database.
<?php
//code to access database
if (isset($_SESSION ['id'])){
session_destroy();
}
session_start();
$id = $_SESSION['id'];
$query = "SELECT * FROM releases WHERE release_id = $id";
$result = mysql_query($query);
if(!$result) die ("Database access failed: " .mysql_error());
$row = mysql_fetch_row($result);
echo "<img src='images/events/" .$row[1] . "' width= '150' height= '150' /> <br />";
echo "track title: " .$row[2] . "<br />";
echo "artist: " .$row[3] . "<br />";
session_destroy();
mysql_close($db_server);
?>
Look at what your code is doing:
for ($j ...) {
$row = mysql_fetch_row($result);
... do stuff with $row;
}
$id = $row[0];
you set $id AFTER the loop has terminated, meaning you will only EVER get the LAST item fetch from the query results.
Even if you were setting $id inside the loop, you'd still be only setting a SINGLE id in $_SESSION anyways:
for ($j ...) {
$id = $row[0];
$_SESSION['id'] = $id;
}
when the loop finishes, $_SESSION will be the last id, yet again. What you're doing does NOT need a session, just a query variable:
for ($j ...) {
$id = $row[0];
echo "<a href='test.php?id=$id'>click me</a>";
^^^^^^^---note the query string....
}
and then in your test.php page:
$id = $_GET['id'];

Mysql Field Data not displaying when a link is clicked?

I'm trying to get data from a database if a link is clicked.
I used the example codes suggested from this example -Getting mysql field data when a link is clicked?
But it doesn't work when I click on a link nothing comes up.
main.php
<?php
include('conn.php');
$sql2 = "SELECT Title FROM addpromo";
$result2 = mysql_query($sql2);
echo "<div id=\"links\">\n";
echo "<ul>\n";
while ($row2 = mysql_fetch_assoc($result2)) {
echo "<li> <a href=\"fullproject.php?title=\""
. urlencode($row2['Title']) . "\">"
. htmlentities($row2['Title']) . "</a>\n</li>";
}
echo "</ul>";
echo "</div>";
?>
This is displaying correct.but when I click at a link nothing is showing up in fullproject.php, Just a blank page.
fullproject.php
<?php
// Connect to server.
include('conn.php');
$projectname = isset($_GET['Title']);
$sql1 = "SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1 = mysql_query($sql1);
while ($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title'] . "<br />";
echo "<br /> ";
}
?>
Can someone help me to fix this, or any other way to make this(to get data from a database if a link is clicked) possible?
Change to this
main.php
<?php
include('conn.php');
$sql2="SELECT Title FROM addpromo";
$result2=mysql_query($sql2);
echo '<div id="links">';
echo '<ul>';
while($row2 = mysql_fetch_assoc($result2)){
echo '<li>'.htmlentities($row2['Title']).'</li>';
}
echo '</ul>';
echo '</div>';
?>
fullproject.php
<?php
if(isset($_GET['title'])){
include('conn.php');
$projectname= $_GET['title'];
$sql1="SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title']. "<br />";
echo "<br /> ";
}
}
?>
This is storing a boolean value $projectname= isset($_GET['Title']);, whether or not the title is set. Instead use $projectname = $_GET['Title'];
isset returns a boolean value (true/false) and you want the actual value of the variable:
$projectname= $_GET['title'];
Furthermore, you have to pass only the title as the URL parameter, without enclosing it within quotes. So there is an error in this line:
echo "<li> <a href=\"fullproject.php?title=" . urlencode($row2['Title']) . "\">"
Note the lack of \" after title=

Create a clickable link in php from sql results

I am trying to make the results from mysql results clickable links in php. I am new to php and please help.
<?php mysql_connect("localhost", "root", "pass") or die(mysql_error());
mysql_select_db("Branches") or die(mysql_error());
$data = mysql_query("SELECT * FROM items order by ID desc Limit 5") or die(mysql_error());
while ($info = mysql_fetch_array($data)) {
echo $info['title'];
echo " <br>";
echo $info['descr'];
echo "<br>";
echo "<br>";
} ?>
while ($info = mysql_fetch_array($data)) {
echo ''.$info['title'].'';
echo " <br>";
echo $info['descr'];
echo "<br>";
echo "<br>";
}
Then in somefile.php, use $_GET to capture the id and show the results
$id = $_GET['id'];
// pull info from db based on $id
$sql = mysql_query('SELECT * FROM items WHERE ID = "'.$id.'"');
.
.
.
.
just use anchor tag like this
while ($info = mysql_fetch_array($data)) {
echo "".$info['title'];. "";
}
echo '$info['descr']'

Edited: Retrieve Table entries by adding ID attribute to file name?

I have a large database of venues - and I would like to display this data in one page that would only change in some sort of an attribute to the id: (Ex: venues.php?id=1, which would get all the data from row #1.)
Edit: Okay, I updated the code and this is what it looks like now:
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".(int)$id) ;
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['VENUE_NAME'] . "</td> ";
Print "<th>Address:</th> <td>".$info['ADDRESS'] . " </td></tr>";
}
Print "</table>";
?>
And upon going to venues.php?id=1 I get this error:
Parse error: syntax error, unexpected T_LOGICAL_OR in
/home/nightl7/public_html/demos/venues/venues.php on line 8
Do you mean something like:
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".(int)$id) ;
In order to "pass" the id into your url "venues.php?id=1"
You need to use a hybrid html/php form with method=get.
You can see an example html form here: w3schools html forms
This is what I would do:
print '<form name="input" action="venues.php" method="get">';
print 'Venue: <select name = "id">';
$con = mysql_connect("","","");
mysql_select_db($dataBase);
if (!$con){die('Could not connect: ' . mysql_error());}
else {
$opt = array();
$optVal = array();
$i = 0;
$sql = "Select * from venues";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$opt[$i] = $row['VenueName'];
$optVal[$i] = $row['VenueID'];
print "<option value='$optVal[$i]'>$opt[$i]</option>";
$i++;
}
}
mysql_close($con);
print '</select><br />';
print '<input type="submit" value="Submit" />';
print '</form>'
This will give you a form that will give you a drop down list of all your venues and once a venue is selected will direct you to the venues.php page with the respective id.
at the top of your venues,php page just use
$id = $_GET['id'];
This assigns the id number to the variable $id and then you can use this "select"
$data = mysql_query("SELECT * FROM venues WHERE id = ".$id) ;
To collect your venue name from your database using the id supplied in the form.
Good Luck :)
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".$id) or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['VENUE_NAME'] . "</td> ";
Print "<th>Address:</th> <td>".$info['ADDRESS'] . " </td></tr>";
}
Print "</table>";
?>

Categories