0

I'm probably asking a very simple question here - I know the basics of calling an array but I think I'm probably not doing it in the most efficient way... I'm calling some data into an array at the start of my page and then I want to be able to use this data-set multiple times throughout the page without wrapping everything in PHP if possible.

At present I'm doing it like this -

A variable ('video') is passed to my page through the URL which I get like so:

<?php 

$video = $_GET['video'];

?>

My <title> tag is pulled from the selected database (also titled 'video')

<?php
$title = mysql_query("SELECT * FROM video WHERE ID = '{$video}'") or die(mysql_error());
    mysql_real_escape_string($video);
    while($head = mysql_fetch_array( $title )) { 
    echo "{$head['title']} - BY XXXXX</title>";
            echo "<meta property=\"og:title\" content=\"{$head['title']} - BY XXXX\"/>";
    }
?>

I then want to use the {$video} data later on the same page, but defining a slightly different variable like so:

<?php
$data = mysql_query("SELECT * FROM video WHERE ID = '{$video}' ORDER BY added DESC") or die(mysql_error());
mysql_real_escape_string($video);
while($info = mysql_fetch_array( $data )) if ($info['ytembed'] == 'yes')  { 
echo "{$info['embedcode']}";
echo "<div class=\"videobox1\">";
echo "<div class='video-title'>{$info['title']}</div>";
echo "<div class='video-subtitle'>{$info['subtitle']}</div>";
echo "<div class='video-credits'>{$info['cast']}</div>";
echo "<div class='back'><a href=\"./\">&laquo;back</a></div></div>";
} else {
echo "no embed code";
}
?>

So at the moment every time I want to pull from that data I'm calling the whole array again - it would be amazing if instead of doing this I could just print/echo selected items

Is there a way to make my code more efficient and do this?

I'm also looking to Validate the ID and if it doesn't exist within the video DB send the user to a 404 page - but perhaps that's a separate question.

2
  • All code in single file right? Commented Oct 22, 2015 at 11:24
  • yeap, all in a single PHP page - just split up in different areas (some in the <head> some in the <body> etc. Commented Oct 22, 2015 at 11:53

1 Answer 1

1

Hello this is refined code

Replace first 1 with this.

$video = $_GET['video'];
$video = mysql_real_escape_string($video);
$videodata = mysql_query("SELECT * FROM video WHERE ID = '{$video}' LIMIT 1") or die(mysql_error());

// execute the query and check if video id exist or not 
if(mysql_num_rows($videodata) == 0){
    // 404 redirect code. 
}

Replace Second with

$videodataArray = array(); // created array for storing video data
while ($head = mysql_fetch_array($videodata))
{
    $videodataArray = $head ;  // store the value in video data array for to use in fulll page
    echo "{$videodataArray['title']} - BY XXXXX</title>";
    echo "<meta property=\"og:title\" content=\"{$videodataArray['title']} - BY XXXX\"/>";
}

Replace last one with

    echo "{$videodataArray['embedcode']}";
    echo "<div class=\"videobox1\">";
    echo "<div class='video-title'>{$videodataArray['title']}</div>";
    echo "<div class='video-subtitle'>{$videodataArray['subtitle']}</div>";
    echo "<div class='video-credits'>{$videodataArray['cast']}</div>";
    echo "<div class='back'><a href=\"./\">&laquo;back</a></div></div>";
Sign up to request clarification or add additional context in comments.

3 Comments

Just given that a go and it seems to have broken the page: jameshenry.info/test/video.php?video=14 For reference the existing code (as above) is here:jameshenry.info/video.php?video=14
Sorry please change $videodataArray = array to $videodataArray = array()
amazing! Working perfectly now - would it be possible for you to give a quick breakdown of what the code is doing?

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.