Lassie Posted March 3, 2011 Share Posted March 3, 2011 I want to retrieve an image id from a db and show the images. I cant get the syntax right for the image tag.Any help appreciated. function display_covers() { global $wpdb; $query = "select * from wp_cover"; $result = mysql_query($query)or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); echo mysql_error(); if (!$result) return false; echo'<div class="wrap"><p>choose from one of the covers below</p></div>'; echo'<div id="main">'; echo'<table class="main" cellpadding="2">'; //echo"<caption>Please choose a book cover</caption>"; ?> <thead><tr><td colspan="5" ><h6 class="main">Book Covers</h6></td></tr> </thead> <?php $i=0; $size=3; echo "<tbody>"; echo "<tr>"; while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { /* display picture */ ?> <td class="main"> <?php echo"<img src="/Applications/MAMP/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/"{.$row['pix'].}""/>"; echo"</td>"; $i++; if($i==$size) { echo "</tr><tr>"; $i=0; } } } Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 3, 2011 Share Posted March 3, 2011 Are you getting any errors? I took a quick look at the code and noticed the following: ... echo"<img src="/Applications/MAMP/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/"{.$row['pix'].}""/>"; ... Should be changed to something like: ... echo '<img src="/Applications/MAMP/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/'{.$row['pix'].}'"/>'; ... Otherwise having a double quote in a double quoted string will throw an error. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 3, 2011 Share Posted March 3, 2011 For reference, you may want to review the manual on strings: http://php.net/manual/en/language.types.string.php Quote Link to comment Share on other sites More sharing options...
Lassie Posted March 3, 2011 Author Share Posted March 3, 2011 Thanks I'll try that. I was getting a syntax error. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 3, 2011 Share Posted March 3, 2011 Sorry, I just noticed something else. You'll also need to remove the curly brackets. Change: <?php ... echo '<img src="/Applications/MAMP/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/'{.$row['pix'].}'"/>'; ... ?> To: <?php ... echo '<img src="/Applications/MAMP/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/'.$row['pix'].'"/>'; ... ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 3, 2011 Share Posted March 3, 2011 echo"<img src=\"/Applications/MAMP/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/{$row['pix']}\" />"; Quote Link to comment Share on other sites More sharing options...
Lassie Posted March 4, 2011 Author Share Posted March 4, 2011 Thanks guys .They both work in terms of getting rid of the syntax error. However my images are not dispalyed. The while loop shows the file names but doesnt show the image. Have I missed something? Code at present function display_covers() { global $wpdb; $query = "select * from wp_cover"; $result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); echo mysql_error(); if (!$result){ return false; } echo'<div class="wrap"><p>choose from one of the covers below</p></div>'; echo'<div id="main">'; echo'<table class="main" cellpadding="2">'; //echo"<caption>Please choose a book cover</caption>"; ?> <thead><tr><td colspan="5" ><h6 class="main">Book Covers</h6></td></tr> </thead> <?php $i = 0; $size = 3; echo "<tbody>"; echo "<tr>"; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { /* display picture */ ?> <td class="main"> <?php echo $row['pix']; echo"<img src=\"/Applications/MAMP/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/{$row['pix']}\" />"; echo"</td>"; $i++; if ($i == $size) { echo "</tr><tr>"; $i = 0; } } } Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 Are you sure your case and path are accurate? Quote Link to comment Share on other sites More sharing options...
Lassie Posted March 4, 2011 Author Share Posted March 4, 2011 yes, i think so. I am using a mac and my web root is htdocs.The main application is wordpress and I am building a plugin to dispay on the admin page. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 what happens when you navigate to http://localhost/Applications/MAMP/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/****.***[code] where the stars are the file name of a know image? Quote Link to comment Share on other sites More sharing options...
Lassie Posted March 4, 2011 Author Share Posted March 4, 2011 http://localhost:8888/Applications/MAMP/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/old_cover.png gives path not valid http://localhost:8888/wordpress_3/wp-content/plugins/Authors2/jackets/old-cover.png shows the image. Can I use a url here? Quote Link to comment Share on other sites More sharing options...
Lassie Posted March 4, 2011 Author Share Posted March 4, 2011 tried that and no go Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 The src="..." attribute in an <img> tag IS A URL because it is the browser that fetches and displays the image. Quote Link to comment Share on other sites More sharing options...
Lassie Posted March 4, 2011 Author Share Posted March 4, 2011 Right.But echo"<img src=\"/http://localhost:8888/wordpress_3/wp-content/plugins/Authors2/jackets/{$row['pix']}\" / still doesnt display my images? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 all you wanted to do to the echo was strip the stuff at the start out that you took of the address to make it work: echo"<img src=\"/Applications/MAMP/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/{$row['pix']}\" />"; should become echo"<img src=\"/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/{$row['pix']}\" />"; Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 Have you checked if the actual src="..." value being output to your browser is what you think it is? And have you forced your browser to clear any cached (broken) content since you changed the code? Quote Link to comment Share on other sites More sharing options...
Lassie Posted March 4, 2011 Author Share Posted March 4, 2011 still no image. If it helps this is the full code as now. function display covers is suppossed to show some covers a user can choose from.Next step would be to pass the id of the selected cover and create an image with narrative overlayed. I also therefore need to both display the image and make it selectable. //function for submenu 5 function cover() { echo'<h2>Please upload your book cover or choose one from the selection</h2> <p>All book ideas require a cover. Your choice can be changed at a later date.</p>'; //confirm book idea has been registered //Get book id use session variable //confirm with book title ?> <div style="text-align: center"> <table width = \"390\" border = 0 align="left"> <tr><td> <form action="" method="post" enctype="multipart/form-data" name="uploadImage" id="uploadImage"> <fieldset> <legend>Upload Your Book Cover</legend> <p> <label for="image">Upload image:<em class="required">(only jpg,gif,png.Max size 4mb)</em></label> <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" /> <input type="file" name="image" id="image" /> </p> <p> <input type="submit" name="upload" id="upload" value="Upload" /> </p></fieldset> </form> </td></tr></table> </div> <br /> <?php display_covers(); //get book info $salt = rand(10000, 99999); $hash = md5($_SESSION[book_id] . $salt); global $wpdb; echo "<br />"; $query = ("SELECT * FROM wp_book WHERE Book_ID = $_SESSION[book_id]"); $result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); echo mysql_error(); if (!$result) { echo"No Result"; } $row = mysql_fetch_array($result, MYSQL_ASSOC); $WorkingTitle = $row['WorkingTitle']; $Author = $row['AuthorName']; $_SESSION[WorkingTitle] = $WorkingTitle; $_SESSION[AuthorName] = $AuthorName; $str = $Author; $str1 = $WorkingTitle; $cover = "old_cover.png"; echo "<img src='http://localhost:8888/test_upload/image-save.php?str=$str&str1=$str1&hash=$hash&cover=$cover'/>"; //save hash value in database $query = "UPDATE wp_book SET BkCover='$hash' WHERE Book_ID='$_SESSION[book_id]'"; $result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); echo mysql_error(); if (mysql_affected_rows() == 1) { // If it ran OK. $message = "You have completed registering your book idea"; //set session variable book_id } if (!mysql_affected_rows() == 1) { echo "Your cover could not be registered"; exit(); } } function display_covers() { global $wpdb; $query = "select * from wp_cover"; $result = mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); echo mysql_error(); if (!$result){ return false; } echo'<div class="wrap"><p>choose from one of the covers below</p></div>'; echo'<div id="main">'; echo'<table class="main" cellpadding="2">'; //echo"<caption>Please choose a book cover</caption>"; echo' <thead><tr><td colspan="5" ><h6 class="main">Book Covers</h6></td></tr></thead>'; $i = 0; $size = 3; echo "<tbody><tr>"; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { /* display picture */ echo'<td class="main">'; echo"<img src=\"/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/{$row['pix']}\" />"; echo"</td>"; $i++; if ($i == $size) { echo "</tr><tr>"; $i = 0; } } } Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 gak, I missed out the /htdocs when I was trimming your url string echo"<img src=\"/wordpress_3/wp-content/plugins/Authors2/jackets/{$row['pix']}\" />"; this time - surely it must work! If it doesn't, please post the FULL url for the page that this code is on. Quote Link to comment Share on other sites More sharing options...
litebearer Posted March 4, 2011 Share Posted March 4, 2011 try a simple test. create a small html file, only content is... <img src="/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/PUT_AN_ACTUAL_IMAGE_NAME_HERE"> also in your php try echo $$row['pix']; ?> <img src="/htdocs/wordpress_3/wp-content/plugins/Authors2/jackets/<?PHP echo $row[pix']; ?>"> <?PHP Quote Link to comment Share on other sites More sharing options...
Lassie Posted March 4, 2011 Author Share Posted March 4, 2011 Right, Thanks guys. I cleared the cache and used the following. echo"<img src=\"/wordpress_3/wp-content/plugins/Authors2/jackets/{$row['pix']}\" />"; Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 4, 2011 Share Posted March 4, 2011 Since it looks like you're using WordPress, have you looked into: <?php bloginfo('template_url'); ?> More information can be found in the Codex: http://codex.wordpress.org/Function_Reference/get_bloginfo Quote Link to comment Share on other sites More sharing options...
Lassie Posted March 5, 2011 Author Share Posted March 5, 2011 Thank you I will look into that. I will need it when I get the basic functionality working. Quote Link to comment Share on other sites More sharing options...
facarroll Posted March 5, 2011 Share Posted March 5, 2011 A little off the point, but you should consider posting the filename of the image to the database and then placing the image in a folder on your site. The retreival should be much faster. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2011 Share Posted March 5, 2011 The filename of the image is in the database and the image is in a folder. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.