Jump to content

NOOB help: Dynamically Embed php


CEinNYC

Recommended Posts

Hello -

 

I'm (very) new to php and working on a project and wondering if anyone can point the direction? (Not necessarily give the answer, but any helpful direction).

 

I need to take YouTube video id's, (built into an array), and post the various video clips.  Also the <object> code is in an php array.

 

(Code Below.. hard to explain).

 

Do I need to convert the array text into a string (as shown), and how do I take that string text and paste it into multiple youtube object posts? Note that the original $embedCode is commented out, '//), so that it can be referenced.

 

Any advice is appreciated!

 

<?php

 

$number = 0;

$youtubeIds = array("MX0D4oZwCsA","2v-v3jh-Cco","WuYDSa4BRaw","zwo48StJSr4","exOxUAntx8I");

// $embedCode = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';

$string = implode($youtubeIds);

$embedCode = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/watch?v='<?php $string?>'?fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/watch?v='<?php $string?>'?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';

 

 

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://opengraphprotocol.org/schema/">

<head>

<title>Youtube Videos</title>

 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<meta name="language" content="en" />

</head>

 

<body>

<h1>Youtube Videos</h1>

 

<p><?php

for($i = 0; $i < count($youtubeIds); $i++)

{

echo $embedCode;

} ?> </p>

</body>

</html>

Link to comment
Share on other sites

you'll need to cycle through a loop and echo out each clip ID/url in the object url.

 

a foreach loop will cycle through every index in an array, no need for count like in your for loop.

 

and then if you wish to use the current embed code string, you'll have to replace __YOUTUBE_ID__ with the corresponding youtube id with str_replace()

 


<?php

$number = 0;
$youtubeIds = array("MX0D4oZwCsA","2v-v3jh-Cco","WuYDSa4BRaw","zwo48StJSr4","exOxUAntx8I");

$embedCode = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/__YOUTUBE_ID__?fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/__YOUTUBE_ID__?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';

$string = implode($youtubeIds);


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://opengraphprotocol.org/schema/">
<head>
<title>Youtube Videos</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
</head>

<body>
<h1>Youtube Videos</h1>

<p><?php
foreach($youtubeIds as $value)
{
//replace
echo '<div>' . str_replace("__YOUTUBE_ID__", $value, $embedCode) . '</div>';
} ?> </p>
</body>
</html>

Link to comment
Share on other sites

Me again.. Thanks for the help earlier.  (and Thanks in advance for any advice).  I don't yet know the keywords to look up when google-digging for direction.

 

Same code as listed above, if I want to list both the title and the youtube id next to each video, is it similar to before?  (foreach).. I tried to embed the operator within the initial foreach, and it all a-came-a-tumbling-down. 

 

 

 

$youtubeMoreChallenging = array( array("rainbow"=>"Double Rainbow Song","id"=>"MX0D4oZwCsA"),

array("mario"=>"Mario and Mr. T Sing By Accident","id"=>"2v-v3jh-Cco"),

array("kids"=>"Cute Kids and Kanye","id"=>"WuYDSa4BRaw"),

array("manning"=>"Manning Brothers Remix","id"=>"zwo48StJSr4"),

array("press"=>"Press Hop","id"=>"exOxUAntx8I")

);

 

Link to comment
Share on other sites

For example:

 

<?php

 

$number = 0;

$youtubeIds = array("MX0D4oZwCsA","2v-v3jh-Cco","WuYDSa4BRaw","zwo48StJSr4","exOxUAntx8I");

 

$embedCode = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';

 

$string = implode($youtubeIds);

 

$youtubeMoreChallenging = array( array("rainbow"=>"Double Rainbow Song","id"=>"MX0D4oZwCsA"),

                            array("mario"=>"Mario and Mr. T Sing By Accident","id"=>"2v-v3jh-Cco"),

                            array("kids"=>"Cute Kids and Kanye","id"=>"WuYDSa4BRaw"),

                            array("manning"=>"Manning Brothers Remix","id"=>"zwo48StJSr4"),

                            array("press"=>"Press Hop","id"=>"exOxUAntx8I")

                          );

 

 

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://opengraphprotocol.org/schema/">

<head>

<title>Youtube Videos</title>

 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<meta name="language" content="en" />

</head>

 

<body>

<h1>Youtube Videos</h1>

 

<ul id="youtubeclips">

<?php

foreach($youtubeIds as $value)

{

echo '<li>' . str_replace("__YOUTUBE_ID__", $value, $embedCode) . '</li>';

echo '<li> <h2>'. ($youtubeMoreChallenging) . '</h2> </li>';

} ?>

 

</ul>

</body>

</html>

Link to comment
Share on other sites

Having an array of arrays, when you foreach, the value is an array. In your case you're adding associative key names to it, but I'm not sure to what purpose.  Having a different key name for every description isn't a benefit that I can see, so I'm going to ignore that for now.  Luckily you can access the array element using it's position as well.

 

  foreach($youtubeMoreChallenging as $value) {
      echo '</pre>
<li>' . str_replace("__YOUTUBE_ID__", $value[1], $embedCode) . '
' . $value[0] . '</li>';<br>  } ?&gt

 

 

Link to comment
Share on other sites

Many thanks!  I follow what you did in the foreach.

 

I think the lesson is simply to operate with arrays.  (You can see from the video clips, this isn't anything serious).  This is for an intro mysql course.  Eventually I'll better learn the syntax in order to research answers on my own.

 

Have a good one!

 

Link to comment
Share on other sites

Hmm..  Working this out.

 

I need to post the video clip (done), and next to the video list out the info in the php $youtubeMoreChallenging.  Code posted below.. Currently I can only get "Array Array Array Array" or "0 1 2 3 4" (as indicated with the code below).  Thank for any direction!  I bet this is allot like watching paint dry to many of the pros on here.

 

 

<?php

 

   

$number = 0;

$youtubeIds = array("MX0D4oZwCsA","2v-v3jh-Cco","WuYDSa4BRaw","zwo48StJSr4","exOxUAntx8I");

$embedCode = '<object width="480" height="385"><param name="movie" value="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/watch?v=__YOUTUBE_ID__?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>';

 

 

 

$string = implode($youtubeIds);

 

 

$youtubeMoreChallenging = array( array("rainbow"=>"Double Rainbow Song","id"=>"MX0D4oZwCsA"),

array("mario"=>"Mario and Mr. T Sing By Accident","id"=>"2v-v3jh-Cco"),

array("kids"=>"Cute Kids and Kanye","id"=>"WuYDSa4BRaw"),

array("manning"=>"Manning Brothers Remix","id"=>"zwo48StJSr4"),

array("press"=>"Press Hop","id"=>"exOxUAntx8I")

);

 

 

 

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://opengraphprotocol.org/schema/">

<head>

<title>YOUTUBE VIDEOS</title>

 

<link rel="stylesheet" type="text/css" media="screen" href="style.css">

 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<meta name="language" content="en" />

</head>

 

<body>

<div class="wrapper">

<div class="header"><br \>

<div class="logo"><img src="youtube-logo.png" border="0" /></div>

<br \><br \><div class="sublogo"><img src="critical.png" border="0" /></div>

</div>

 

<div class ="content">

 

 

<ul id="youtubeclips">

<?php

foreach($youtubeIds as $value) {

echo '<li>' . str_replace("__YOUTUBE_ID__", $value, $embedCode) . '</li>';

 

foreach($youtubeMoreChallenging as $key => $title){

echo '<li> <h2>'. ($key) . '</h2> </li>';

}

 

 

 

} ?>

</ul>

 

 

 

</div>

</body>

 

 

 

<div class="footer"> </div>

</div>

 

</html>

 

 

Link to comment
Share on other sites

now since you're using a 2 dimensional array ($array['index']['index2']) your foreach loop is returning $array['index'],

you are having the index of the 2nd dimensional array as the name of the youtube clip ("rainbow"=>"Double Rainbow Song"), instead you should split that up into 2 index's, such as "clipName" => "Double Rainbow Song", "clip" = > "rainbow", "id"=>"MX0D4oZwCsA" that way you can easily pull array values.

 

keep the foreach, adjust the arrays as stated above, and then in the foreach you'll be able to pull values like so

               <?php
               foreach($youtubeMoreChallenging as $value) {
               echo '<li>' . str_replace("__YOUTUBE_ID__", $value['id'], $embedCode) . '</li>';

//echo name
echo $value['clipName'];

//echo clip 
echo $value['clip'];               
               
               
               } ?> 

Link to comment
Share on other sites

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.