Jump to content

display images with a variable


Lassie

Recommended Posts

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

}
}


Link to comment
https://forums.phpfreaks.com/topic/229498-display-images-with-a-variable/
Share on other sites

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.

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'].'"/>';
...
?>

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

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']}\" />";

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


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.

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.