Lassie Posted February 7, 2008 Share Posted February 7, 2008 I have a function which diplays details about a book with an image. I want to make the image clickable and refenece the 'show details' page. The code goes through a foreach loop to display each book in a category. I cant seem to write a valid a ref. I have tried the following:- /* display picture */ echo "<td><a href='show_book.php?product_id='.($row{'product_id'})> <img src= './images/{$row['pix']}' border='0' width='100' height='80'></a></td>\n"; The function is as follows function display_books($book_array) { //display all books in the array passed in if (!is_array($book_array)) { echo '<br />No books currently available in this category<br />'; } else { //create table echo '<div style="text-align: center">'; echo '<table width = \"700\" border = 0 align="center">'; //create a table row for each book foreach ($book_array as $row) { $url= 'show_book.php?product_id='.($row{'product_id'}); $title = $row['title']; echo '<tr><td>'; do_html_url($url,$title); echo '</td></tr>'; echo "<tr>\n"; echo "<td>{$row['product_desc']}</td>\n"; /* display picture */ echo "<td><a href='show_book.php?product_id='.($row{'product_id'})> <img src= './images/{$row['pix']}' border='0' width='100' height='80'></a></td>\n"; echo "<tr><td colspan='5'><hr></td></tr>\n"; } echo "</tbody></table>\n"; echo '</div>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/89932-solved-how-to-included-variable-in-a-tag-and-image/ Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 Shouldn't <a href='show_book.php?product_id='.($row{'product_id'})> be <a href='show_book.php?product_id={$row['product_id']}'> Quote Link to comment https://forums.phpfreaks.com/topic/89932-solved-how-to-included-variable-in-a-tag-and-image/#findComment-461027 Share on other sites More sharing options...
Lassie Posted February 7, 2008 Author Share Posted February 7, 2008 I take it you mean in the display image code. I tried your version but it does not append the product code. Quote Link to comment https://forums.phpfreaks.com/topic/89932-solved-how-to-included-variable-in-a-tag-and-image/#findComment-461043 Share on other sites More sharing options...
The Little Guy Posted February 7, 2008 Share Posted February 7, 2008 I was going to say shouldn't this: <a href='show_book.php?product_id='.($row{'product_id'})> Be this: <a href='show_book.php?product_id='.display_books($row{'product_id'})> Quote Link to comment https://forums.phpfreaks.com/topic/89932-solved-how-to-included-variable-in-a-tag-and-image/#findComment-461048 Share on other sites More sharing options...
Lassie Posted February 8, 2008 Author Share Posted February 8, 2008 Thanks. Again no product code appended This piece works fine to get the books from the book_array and displayed.The title is clickable, but most people like to click on the image hence why I am trying to get the link to work. foreach ($book_array as $row) { $url= 'show_book.php?product_id='.($row{'product_id'}); $title = $row['title']; echo '<tr><td>'; do_html_url($url,$title); echo '</td></tr>'; Quote Link to comment https://forums.phpfreaks.com/topic/89932-solved-how-to-included-variable-in-a-tag-and-image/#findComment-461629 Share on other sites More sharing options...
aschk Posted February 8, 2008 Share Posted February 8, 2008 I'm just curious to know if that associative id actually exists .... do a print_r($book_array) and let us know what you see Quote Link to comment https://forums.phpfreaks.com/topic/89932-solved-how-to-included-variable-in-a-tag-and-image/#findComment-461630 Share on other sites More sharing options...
Lassie Posted February 8, 2008 Author Share Posted February 8, 2008 The array is populated and the other functionality all works fine. The array contained Array ( [0] => Array ( [product_id] => 2 [cat_id] => 6 [title] => 50 Salad Delights [product_desc] => Never be at aloss for a salad idea again [price] => 1.99 [pix] => saladrecipes.jpg [type] => Food & Drink [added_date] => 0000-00-00 [author] => Hawkesley [isbn] => [Featured] => 1 [New] => [business] => 0 [promo_link] => [dl_link] => Salad Recipes.pdf ) [1] => Array ( [product_id] => 6 [cat_id] => 6 [title] => How to bake the perfect loaf [product_desc] => Rises every time [price] => 2.99 [pix] => bread.jpg [type] => Food & Drink [added_date] => 0000-00-00 [author] => Stocks [isbn] => [Featured] => 1 [New] => [business] => 0 [promo_link] => [dl_link] => How to Buy a Car With Little or no credit.pdf ) [2] => Array ( [product_id] => 13 [cat_id] => 6 [title] => Catering 4 Cash [product_desc] => Start your own Catering business with this step by step practical guide. [price] => 19.95 [pix] => Book Jacket95x141.gif [type] => Food & Drink [added_date] => 2006-10-30 [author] => Ray Pocock [isbn] => 45129 [Featured] => [New] => [business] => [promo_link] => [dl_link] => How to Buy a Car With Little or no credit.pdf ) ) Quote Link to comment https://forums.phpfreaks.com/topic/89932-solved-how-to-included-variable-in-a-tag-and-image/#findComment-461654 Share on other sites More sharing options...
laffin Posted February 8, 2008 Share Posted February 8, 2008 it's cuz the arrays you are searching for are in an array. Reason the foreach works solution create a function to return the single array from the array function get_item($id) { global $book_array; $ret=array(); foreach($book_array as $row) if($row['product_id']==$id) $ret=$row; return $ret; } now ya can search for the product id and get the right array. it will return an empty array if not found so this will work if(!empty($row=get_item(13)) { echo "Product: $row[title]<br>"; } else { echo "Product not found<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/89932-solved-how-to-included-variable-in-a-tag-and-image/#findComment-461711 Share on other sites More sharing options...
Lassie Posted February 8, 2008 Author Share Posted February 8, 2008 Thanks for coming back. I am not certain I follow the logic. To be clear at present I have the function display_books($book_array) This is called when a book category is selected. The foreach then goes throught the array ($book_array as $row) so I have a list of books, comprising title ,product description and the book jacket image. The title is clickable and adds the product_id (show_book.php?product-id='.($row('product_id')). I want to add the same code to the image tag so that from the list the book details are displayed. This another function display_book_details($book). What I cant see is how to use the function you suggest from within the existing display_book function. Sorry to be slow on this but I would like to understand it before coding. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/89932-solved-how-to-included-variable-in-a-tag-and-image/#findComment-461866 Share on other sites More sharing options...
laffin Posted February 8, 2008 Share Posted February 8, 2008 it's meant to be used outside of the function. if you build something within the function, it has to be located within the foreach operation. with all the table formatting code, ya will have to be specific where your image goes. which i assume is this /* display picture */ echo "<td><a href='show_book.php?product_id='.($row{'product_id'})> <img src= './images/{$row['pix']}' border='0' width='100' height='80'></a></td>\n"; $row is an array, yet you are using curly braces, which is wrong, and the quotes are not being followed. Curly braces work in php5 not in php4. square brackets are used in an array /* display picture */ echo "<td><a href='show_book.php?product_id={$row['product_id']}'> <img src= './images/{$row['pix']}' border='0' width='100' height='80'></a></td>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/89932-solved-how-to-included-variable-in-a-tag-and-image/#findComment-461900 Share on other sites More sharing options...
Lassie Posted February 9, 2008 Author Share Posted February 9, 2008 Thank you. I tried that and it has the desired result. I follow what you are saying re the array. Quote Link to comment https://forums.phpfreaks.com/topic/89932-solved-how-to-included-variable-in-a-tag-and-image/#findComment-462525 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.