Jump to content

Trying to create a loop function for a sequenced list


natez311

Recommended Posts

Hi Guys,

 

This forum is pretty sweet.  I was looking around and figured I'd post here with a recent problem I'm currently having since I have very little knowledge of php.

 

My current issue:

 

I have a list of items all listed in specific order using php commands to call information.  A lot this code seems to repeat itself. I believe there is a much better way of coding what I've got setup.

 

Here is an example of a couple of items within the list:

 

<li><a href="<?php echo $url_flashVid ?>/videoPlayer.php?name=1" id="mb1" class="mb" rel="width:500,height:399" title="JUX"><img src="<?php echo $url_home ?>/images/1.jpg" alt="thumb_vid_1" /></a>
	<h5><a href="<?php echo $url_flashVid ?>/videoPlayer.php?name=1" id="mb1" class="mb" title="JUX">JUX Scene</a></h5>
	<p class="caption">	01:59</p>
	<div class="multiBoxDesc mb1">A scene from JUX</div>
</li><!-- /listItem -->
<li><a href="<?php echo $url_flashVid ?>/videoPlayer.php?name=2" id="mb2" class="mb" title="Who is Taj Mahal"><img src="<?php echo $url_home ?>/images/2.jpg" alt="thumb_vid_1" /></a>
	<h5><a href="<?php echo $url_home ?>/media/video/whoisTaj_trailerPT2.mov" id="mb2" class="mb" title="Who is Taj Trailer">Who is Taj</a></h5>
	<p class="caption">00:43</p>
	<div class="multiBoxDesc mb2">Who is Taj? </div>
</li><!-- /listItem -->

 

 

 

What I would like to do is make this more automatic.  Most likely use a loop to spit out information stored in an array.  The example below I think is how the code should really look with everything setup properly.  The "automatic solution" would increase the value of each variable within the array by +1 and the correct information from the array I have setup would be called:

 

Array (stored in a config file or database):

$videoPop[0] = Title 1
$videoPop[1] = Title 2

 

What list would look like after spit out through a function:

<li><a href="$videoPop[0]" id="$mbId[0]" class="mb" rel="$vidDimension[0]" title="$vidtitle[0]"><img src="$imgTmb[0]" alt="$altPic[0]" /></a>
	<h5><a href="$videoPop[0]" id="$mbId[0]" class="mb" title="$vidTitle[0]">$vidTitle[0]</a></h5>
	<p class="caption">$vidCap[0]</p>
	<div class="multiBoxDesc mb1">$vidDescrip[0]</div>
</li><!-- /listItem -->
<li><a href="$videoPop[1]" id="$mbId[1]" class="mb" rel="$vidDimension[1]" title="$vidtitle[1]"><img src="$imgTmb[1]" alt="$altPic[1]" /></a>
	<h5><a href="$videoPop[1]" id="$mbId[1]" class="mb" title="$vidTitle[1]">$vidTitle[1]</a></h5>
	<p class="caption">$vidCap[1]</p>
	<div class="multiBoxDesc mb1">$vidDescrip[1]</div>
</li><!-- /listItem -->

 

Trying to find the best place to start.  Any advice or suggestions would be great.  Thanks in advance!

Once you get the items in your array, you could output them like this

 

<?php for($1 = 0; $i < count($videoPop); $i++) { ?>
<li><a href="<?php echo $videoPop[$i]; ?>" id="<?php echo $mbId[$i]; ?>" class="mb" rel="<?php echo $vidDimension[$i]; ?>" title="<?php echo $vidtitle[$i]; ?>"><img src="<?php echo $imgTmb[$i]; ?>" alt="<?php echo $altPic[$i]; ?>" /></a>
      <h5><a href="<?php echo $videoPop[$i]; ?>" id="<?php echo $mbId[$i]; ?>" class="mb" title="<?php echo $vidTitle[$i]; ?>"><?php echo $vidTitle[$i]; ?></a></h5>
      <p class="caption"><?php echo $vidCap[$i]; ?></p>
      <div class="multiBoxDesc mb1"><?php echo $vidDescrip[$i]; ?></div>
</li><!-- /listItem -->
<?php } ?>

Basically what matthewj posted but i have some links to the php manual for you too. code is almost the same.

 

ok, your original code doesn't really corresspond with what you want to achieve since you are using the same variables for each different link or item.

 

if you want to "automatically" output different pieces of data into a "template" of HTML then using arrays is a good way to it like you've said.

 

You want to look at the for () and foreach () control structure as well as the array types:

http://au2.php.net/manual/en/control-structures.for.php

http://au2.php.net/manual/en/control-structures.foreach.php

http://au2.php.net/manual/en/language.types.array.php

 

Once you have all the data in the array you want, use a for loop:

 

Personally, I would start off with something like this:

$output = '<html><head><title>php output items</title></head><body>'."\n";
$output .= '<ul>'."\n";
for ($key = 0; $key < count($vidTitle); $key++)
{
   $output .= '<li><a href="'.$videoPop[$key].'" id="'.$mbId[$key].'" class="mb" rel="'.$vidDimension[$key].'" title="'.$vidtitle[$key].'"><img src="'.$imgTmb[$key].'" alt="'.$altPic[$key].'" /></a>'."\n"
      .'<h5><a href="'.$videoPop[$key].'" id="'.$mbId[$key].'" class="mb" title="'.$vidTitle[$key].'">'.$vidTitle[$key].'</a></h5>'."\n"
      .'<p class="caption">'.$vidCap[$key].'</p>'."\n"
      .'<div class="multiBoxDesc mb1">'.$vidDescrip[$key].'</div>'."\n"
   .'</li><!-- /listItem -->'."\n";
}
$output .= '</ul>'."\n".'</body></html>'."\n";
print($output);

 

This should get you started.

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.