I have an issue on how to create individual php pages automatically.
I have already created a page called catalog.php.
In catalog.php, mySQL query would take place, querying:
$link = mysql_connect("localhost", "root", "");
mysql_select_db("photodb", $link);
$sql = "SELECT id, title, caption, comments, imagelink, year FROM photo";
Then this query would loop and display the contents on catalog.php side by side:
<ul class="grid">
<?php while ($row = mysql_fetch_assoc($result))
{ ?>
<li>
<p class="image"><img src="getImage.php?id=<?php echo $row['id']; ?>" alt="" width="175" height="200" /></p>
<p class="name"><?php echo $row['title']; ?></p>
<p class="year"><?php echo $row['year']; ?></p>
</li>
<?php } // while
?>
</ul>
where class="grid" would arrange all the queried data side by side, with image, title and year being displayed.
However, one requirement that i need is that whenever i click on any of these images, it should link to its own php page(individual.php) to show a detailed image, title, caption and author's comments. An elaboration is shown below:
Title
IMAGE Caption
Author's comments
An example:
Picture of bridge
IMAGE This image was taken in paris
Low Shuttle Speed
In the above, IMAGE, caption, title, author's comments are found in the same database "photodb" in the same table "photo"
My issue here is as follows:
Is there a way to create such pages automatically? The reason being is, if I create them manually I would have a hard time because my database has more that 100 entries.
In the <a href=""> tag as seen in catalog.php, what should the values be?
I already have a template for such a "individual.php" page. An example of he structure is follows:
<body>
<p class="title">
London Bridge</p>
<p class="caption">
Image taken in summer 2009<br /><br /></p>
<p class="image">
<img border="0" class="floatleft" src="imagelink" width="250" height="400" />
Low Shuttle Speed
</p>
</body>
How can i change this structure to suit my requirements?
Four. Can such "individual.php" url be renamed to something else with a unique id? Each image found in the database has their own unique ID.
Five. I have already queried the database in catalog.php. Can i somehow reuse this query individual.php?
In case you are wondering, getimage.php is as follows:
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("photodb", $link);
$sql = "SELECT imagelink FROM photo WHERE id=$id";
$result = mysql_query($sql, $link);
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo file_get_contents($row['imagelink']);
?>
Many thanks for reading my questions.
Your last code snippet essentially answered your question - you'll create another page and pass a variable to it through the URL ($_GET). So your URL may look like "individual.php?id=25" which would pull the image with the ID of '25'. $_GET['id'] would be '25' in this example.
I also noticed you are using the "root" user for the MySQL connection. I would strongly advise against that.
http://www.1stwebdesigner.com/tutorials/getting-started-php-dynamic-content/
http://www.greensql.com/articles/mysql-security-best-practices
With reference to #Nathan Loding. I would also use
mysql_real_escape_string()
Obviously to prevent mySQL attacks. Judging by some of your code above, I guess your new or an "OLDISH" weby copy and paste coder??
These functions will help and help you learn too.. very easy to do this... so..yes ..You can use file functions.
fopen()
See also Appendix M, fclose(), fgets(), fread(), fwrite(), fsockopen(), file(), file_exists(), is_readable(), stream_set_timeout(), popen(), and stream_context_create().
Related
I am trying to display an image. I do this by fetching the link to the online image and passing it to a variable that then goes into the "src" of the <img> tag. I have tried doing this several ways, but nothing works.
I know how to make this work with folders and downloaded images, but I'd like to do this with online links. like google images.
NOTE: links are put into the database, and when I print_r($plaatje) it shows the actual link. but when I try to paste it into the HTML element, it doesn't work. I feel like I am making a simple mistake, but I can't find it.
$plaatje_link = $connect->prepare("SELECT `plaatje_link` FROM `test`
WHERE plaatje_id = 1");
$plaatje_link->execute();
$plaatje = $plaatje_link->fetch();
array_shift($plaatje);
implode($plaatje);
?>
<div style="height: 100%; width: 100%;">
<img src="<?php echo $plaatje ?>" alt="Ja, foto wil niet"/>
</div>
P.S. the array_shift is there because for some reason it gives me the lik twicw. so with array_shift it removes one and leaves me with 1 value, which I need.
implode($plaatje); works by value, not reference. The original array is not modified. So after this implode operation $plaatje is still the array, not a string.
Also ->fetch() has different modes, default being 'BOTH', meaning it will return the result as an array with both the numbered indexes as well as associative. See PDOStatement::fetch for more info.
You will have to either :
$plaatje = implode($plaatje); // convert it to a string ** for the specific example at least
Or Change
<img src="<?php echo $plaatje ?>" alt="Ja, foto wil niet"/>
To
<img src="<?php echo $plaatje['plaatje_link']?>" alt="Ja, foto wil niet"/> // or $plaatje[0]
Because you can't echo an array.
I am facing a problem in showing images in my page using PHP. I have a table "images" in my db. I have saved the names of the images in this table which i am retrieving using PHP. My problem is that the images are not being shown due to unknown logical error. I have given both the absolute and the relative paths of the images in the tag but all in vain. Please suggest me what wrong i am doing in retrieving the code.
My PHP:-
<?php
require_once("includes/database.php");
$img_no = $_GET["img_no"];
$query = "SELECT * FROM `images` WHERE `img_no`=".$img_no;
$result = mysqli_query($con, $query);
?>
HTML:-
<div class="project owl-carousel">
<?php while($row = mysqli_fetch_array($result)){ ?>
<div class="item">
<?php echo "<img class='img-responsive' src='img/".$row['img']."png'> ";?>
</div>
<?php }; ?>
</div>
In the img src, I have given both the absolute and the relative paths of the images folder, but did not work!
Please suggest!
Thanks in advance!
Regards!
Does your image extention(.png) is same to what you have written in your code.
if yes then check whethere you have saved your image name in database with extention or without it.
I'm using the lightbox plugin: Fancybox
I wanted to apply lightbox effect on image gallery php file. The image is stored in longblob format instead of folder. Following is the code I did to read image.
<?php
$img_slct=mysql_query("SELECT * FROM news_gallery WHERE news_id='$page[news_id]' ORDER BY position ASC");
while($img=mysql_fetch_array($img_slct))
{ ?>
<img src="load_image.php?id=<?php echo $img['file_id']; ?>"/>
<?php } ?>
load_image.php
$id=$_GET['id'];
$is_file=false;
if(!empty($id))
{
$file=mysql_query("SELECT id FROM news_files WHERE id='$id'");
if(mysql_num_rows($file)>0)
{
$is_file=true;
$file=mysql_fetch_array($file);
}
}
if($is_file)
display_file_news($id);
Everything is displayed correctly but that is an issue when using lightbox to display the image retrieved from longblob database.
<a rel="example_group" href="load_image.php?id=<?php echo $img['file_id']; ?>">
<img src="load_image.php?id=<?php echo $img['file_id']; ?>"/>
</a>
I have no idea how to read the longblob database using lightbox. So I simply put like this --> href="load_image.php?id=<?php echo $img['file_id']; ?>"
I know that is wrong, but can you help me? Please...Let me know if you need more info, because I can't insert all my code at here. Thank you.
Following are my database tables.
news_files table
news_gallery table
This question already has answers here:
php - insert a variable in an echo string
(10 answers)
Closed 4 years ago.
I am trying to do something I know is probably simple, but I am having the worst time.
I have functioning so far:
1.Script to upload image files to server
2. write the image file names to the database
3. I want to retrieve the image filename from the db and add it to the img src tag
here is my retrieval script
<?php
$hote = 'localhost';
$base = 'dbasename';
$user = 'username';
$pass = '******';
$cnx = mysql_connect ($hote, $user, $pass) or die(mysql_error ());
$ret = mysql_select_db ($base) or die (mysql_error ());
$image_id = mysql_real_escape_string($_GET['ID']);
$sql = "SELECT image FROM image_upload WHERE ID ='$image_id'";
$result = mysql_query($sql);
$image = mysql_result($result, 0);
header('Content-Type: text/html');
echo '<img src="' $image'"/>';
?>
I was trying to pass the Value through image.php?ID=2 but no luck
The PHP script successfully returns the filename, but I cannot for the life of me get it to print it to the html
Any suggestions, please and thank you very much :)
OK, it does return the proper tag, but now it seems as though the script doesnt run to generate the tag.
I have tried two ways:
<div class="slides">
<div class="slide">
<div class="image-holder">
<?php
include ("image.php?ID=2");
?>
</div>
and:
<img src="image.php?ID=2" alt="" />
but neither one will insert the filename...
I need to identify each img src by the primary key, so I was passing it the ID from each image src location
but alas, my PHP ninja skills need to be honed.
Just to clarify: I am uploading images to the server, recording the filenames in a DB and calling that filename in an HTML doc...there are several in each one so I need to pass the ID (i.e. 1,2,3 ) to correspond to the primary key in the table.
But I cant get the script to process the tag first.
If I go to view source, I can click the script and get the proper result...
Thanks again, you guys and girls are very helpful
You're missing the concatenation operator: .:
echo '<img src="' . $image . '"/>';
You can do it as you did but you had the single quotes in twice, (unless you were meaning to use concatenation - which is unnecessary - if you want this see the other answer).
echo "<img src=\"$image\"/>";
Or the longer form with braces if you need to embed inside text.
echo "<img src=\"${image}\"/>";
I'd recommend using heredoc syntax for this if you're doing lots of HTML. This avoids the need to have lots of echo lines.
echo <<< EOF
<div class="example">
<img src="$image" />
</div>
EOF;
Try using this syntax:
echo "<img src=\"$imagePath\" />";
It works with double quotes provided you escape the quotes in the src attribute. Still not sure why the singles don't work.
there are 2 major flaws with your design
There is no image.php?ID=2 file on your disk.
There is absolutely no point in including image.php file. You have to get the name right in the file you are working with. don't you have this row already selected from the database? Why not just print the image name then?
And yes, you are using single quotes where double ones needed.
I am writing a script to display images on my site. The images are stored on the server and the source (path) is on a mySQL database. I want to be able to move to the next (or previous picture) by invoking two php functions I have made to do so. The code I have written so far is:
<?php
require "db_connection.php";
$query="SELECT source FROM photos";
$result=mysql_query($query);
$count=0;
$numberofpics=mysql_num_rows($result);
$image=mysql_result($result, $count, "source");
$num=1;
function slideshowForward() {
$num=$num+1;
if($num==($numberofpics+1)) {
$num=1;
}
$count=$count+1;
$image=mysql_result($result, $count, "source");
}
function slideshowBack() {
$num=$num-1;
if ($num==0) {
$num=$numberofpics;
}
$count=$count-1;
$image=mysql_result($result, $count, "source");
}
?>
The html portion to display the images is:
<!-- FORWARD AND BACK FUNCTIONS-->
<a class="back" href="http://mywebsite.com/discoverandrank.php?function=slideshowBack()"> <img src="graphics/back.png"/></a>
<a class="next" href="http://mywebsite.com/discoverandrank.php? function=slideshowForward()"><img src="graphics/forward.png"/></a>
<!--DISPLAY MIDDLE PHOTO-->
<div id="thepics">
<img src="http://www.mywebsite.com/<?php echo $image; ?>" name="mypic" border=0 height="300" width="500"></img>
</div>
I'm pretty sure that the php script is incrementing/decrementing the count currently for the image, but I think the problem might be because the html (specifically img src="....") part is not re-evaluated when the count of the image increases?
Mmm there are two things to point out here:
I think that passing a function name as a parameter on an action is not a good idea, instead you should user something like: "http://mywebsite.com/discoverandrank.php?op=next¤t=10" and then evaluate the op and current value to take some action.
For gettting the next picture from database you could use something like this:
"Select source FROM photos LIMIT "current+1" , 1"
For gettting the previous picture from database you could use something like this:
"Select source FROM photos LIMIT "current-1" , 1"
This way you dont need to keep counters and stuff, just have to check if those queries return any data.
Hope this helps!