Jump to content

[SOLVED] How to included variable in a tag and image


Recommended Posts

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>';
  	}
}

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>';

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
        )

)

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>";
}

 

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.

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";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.