0

I have a page and I would really like some help / advice to create a better way / function to call in information from another table.

At the moment, my code looks like: [I do know this is deprecated SQL and would really like to do some nice SQLi for this.]

<?
$menuid = "100";
$imageid = "50";

// ** Talk to 'imagedirectory' table

mysql_select_db($database_db, $BASEdb);
$query_displayimage = "SELECT * FROM imagedirectory WHERE menuid = ".$menuid." AND imageid = ".$imageid."";
$displayimage = mysql_query($query_displayimage, $BASEdb) or die(mysql_error());
$row_displayimage= mysql_fetch_assoc($displayimage);

?>

<img src="/images/assets/<?php echo $menuid; ?>-<?php echo $imageid; ?>-<?php echo $row_displayimage['urlslug']; ?>.jpg" alt="<?php echo $row_displayimage['alttext']; ?>" />

I figure There really has to be a better way because if there is 10 images on a page, this is pretty intense way of doing it.

6
  • What do you intend to gain from this question? When you already know that mysql_*() functions are deprecated and using MySQLi is the way to go, you got everything you need to educate yourself. For example here: php.net/mysqli Commented Nov 12, 2014 at 15:32
  • hi @icefyre - I appreciate your comments, I am sure I will be to Google and replicate the above code in SQLi without too much trouble I'd imagine. The thing I don't want to do is actually replicate that code. There has to be a better way to do this via a function of some sort, though I don't really know where to begin. What I am after is some help with the logic. Commented Nov 12, 2014 at 15:42
  • I understand. However, you only provided a code without any information about the logic. What is the purpose of your script? Commented Nov 12, 2014 at 15:45
  • I might adjust my question a little to be more specific. Though in essence I go through the above code to get the 'urlslug' and 'alttext' fields from a 'imagedirectory' table to display an image. The problem is that if I have 10 images on a page I literally have to repeat the above code 10x, I would like to have it so I have a function where I can easily tell the request the 'menuid' and 'imageid' and it spits out a result: <img src="/images/assets/ $menuid-$imageid-$urlslug.jpg" alt="$alttext" /> Commented Nov 12, 2014 at 15:58
  • Apologies on the lack of detail/direction of my question. Commented Nov 12, 2014 at 15:59

1 Answer 1

1

Since you seem to know that mysql_* is deprecated, I am assuming you have read up on, and are using mysqli_* instead.

You needn't query the database every time. mysqli_query() returns a mysqli_result, which you can iterate over, and read using functions like mysqli_fetch_assoc(). Here is one way of doing it:

<?php

// store your query in a variable.
$query_displayimage = "SELECT * FROM imagedirectory";

// query the database.
$displayimage = mysqli_query($query_displayimage, $BASEdb);

// check for errors.
$dbErrors = mysqli_error($BASEdb);
if (count($dbErrors))
{
    print_r($dbErrors);
    die();
}

// iterate over the returned resource.
while ($row_displayimage = mysql_fetch_assoc($displayimage))
{
    echo '<img src="/images/assets/' . $menuid . '-' . $imageid . '-' . $row_displayimage['urlslug'] . '.jpg" alt="' . $row_displayimage['alttext'] . '" />';
}

?>

Hope that helped.


EDIT:

You can use this code in a function, too. For example:

<?php

function printImage($menuid, $imageid)
{
    $query_displayimage = "SELECT * FROM imagedirectory";
    $displayimage = mysqli_query($query_displayimage, $BASEdb);
    $dbErrors = mysqli_error($BASEdb);
    if (count($dbErrors))
    {
        print_r($dbErrors);
        die();
    }
    
    if ($row_displayimage = mysql_fetch_assoc($displayimage))
    {
        echo '<img src="/images/assets/' . $menuid . '-' . $imageid . '-' . $row_displayimage['urlslug'] . '.jpg" alt="' . $row_displayimage['alttext'] . '" />';
    }
    else    // if there is a problem getting the image
    {
        echo 'Error getting image.';
    }
}

?>

and elsewhere in your HTML, you would do something like:

<div>
    And here is an image!
    <?php printImage(20, 50); ?>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.