Jump to content

[SOLVED] newbie help on multidimensional array in oop


sw9

Recommended Posts

Hi,

 

I'm still learning oop (very new), and I'm trying to create multi-dimensional arrays to return three values. I know there are three values available, but when I try to incorporate it into the multidimensional array it only shows one result. So it seems like the code is working right, but I don't get why it only displays a single result instead of 3. Here is my code:

 

<?php class Related {
	public $relatedVideos;
	public $type;
                public $vids = array();

         function __construct(){
		$this->relatedVideos = '';
		$this->type = ''; 
		$this->vids = $array; 
		}

      function getRelatedSeries($fnid, $numThumb){
      	
      	$sql = "SELECT n.nid, n.title as title, cp.field_poster_thumbnail_frame_value as thumb
      						FROM node n LEFT JOIN content_type_content_program cp ON (n.nid = cp.nid)
      						WHERE cp.field_series_nid = '".$fnid."'
      						ORDER BY cp.field_pdate_value DESC LIMIT ".$numThumb."";
      	$result = db_query($sql) or die($sql);
      
      
      	while ($node = db_fetch_object($result)) {
      	$vids[0]->title = $node->title;
        $vids[0]->nid = $node->nid;
      	} // End While
      	return $vids;
      } // End function
      
  }  // End Class

$rv = new Related();
$vids = $rv->getRelatedSeries(43178, 3); 
      
foreach ($vids as $n){

echo $n->nid ." - ". $n->title;
} // End foreach  
?>

 

Help is greatly appreciated!

Try this:

 

<?php class Related {
	public $relatedVideos;
	public $type;
                public $vids = array();

         function __construct(){
		$this->relatedVideos = '';
		$this->type = ''; 
		$this->vids = $array; 
		}

      function getRelatedSeries($fnid, $numThumb){
      	
      	$sql = "SELECT n.nid, n.title as title, cp.field_poster_thumbnail_frame_value as thumb
      						FROM node n LEFT JOIN content_type_content_program cp ON (n.nid = cp.nid)
      						WHERE cp.field_series_nid = '".$fnid."'
      						ORDER BY cp.field_pdate_value DESC LIMIT ".$numThumb."";
      	$result = db_query($sql) or die($sql);
      
        $counter = 0;
      	while ($node = db_fetch_object($result)) {
      	$vids[$counter]->title = $node->title;
        $vids[$counter]->nid = $node->nid;
        $counter++;
      	} // End While
      	return $vids;
      } // End function
      
  }  // End Class

$rv = new Related();
$vids = $rv->getRelatedSeries(43178, 3); 
      
foreach ($vids as $n){

echo $n->nid ." - ". $n->title;
} // End foreach  
?>

 

You were overwriting the array key each time.

Sorry, I thought this was solved but then realized I'm still having a problem with one of the variables in my while loop.

 

Each time the loop goes, it adds the next value onto the $this->mvpath variable instead of replacing it. I thought just setting $this->mvpath='' would take care of this, but it still keeps happening. Can anyone suggest a way to ensure it gets written OVER, instead of just getting added on? This variable is in the second class, and it runs a function from the top class.

 

My code:

 

<?php 
class RootFilename {

public $RootFilename;

function __construct(){
$this->RootFilename = '';
}

function getPath($MVPath){
     // Extract root filename from Maestrovision path
     $this->RootFilename .= substr($MVPath, strrpos($MVPath, "\\") + 1, strrpos($MVPath, ".") - strrpos($MVPath, "\\") - 1);
     // Convert "&" to "%26" to preserve valid URLs
     $this->RootFilename .= str_replace("&", "%26", $RootFilename);
     
     return $this->RootFilename;
}
}

// This class gets related videos for the related videos block.
class RelatedVideos extends RootFilename {
public $relatedVideos;
public $type;
        public $vids = array();
        public $thumbnailServer;
public $mvpath;


         function __construct(){
		$this->relatedVideos = '';
		$this->type = ''; 
		$this->vids = $array; 
		$this->thumbnailServer = 'http://cctv.dreamhosters.com/streaming/archive/';
		$this->mvpath = '';
		}

      function getRelatedSeries($featured_nid, $series, $numThumb){
      	
      	$sql = "SELECT n.nid, n.title as title, cp.field_poster_thumbnail_frame_value as thumb, cp.field_mv_path_value as mvpath
      						FROM node n LEFT JOIN content_type_content_program cp ON (n.nid = cp.nid)
      						WHERE cp.field_series_nid = '".$series."' AND n.nid != '".$featured_nid."' 
      						ORDER BY cp.field_pdate_value DESC LIMIT ".$numThumb."";
      	$result = db_query($sql) or die($sql);
        $counter = 0;
      	while ($node = db_fetch_object($result)) {
      	

// THIS IS THE VARIABLE THAT DOESN'T CLEAR AS THE LOOP RESTARTS. IT IS CALLING THE FUNCTION FROM THE CLASS IT EXTENDS
      	$this->mvpath = '';
        $this->mvpath = $this->getPath($node->mvpath);
      	// Get the pathauto alias to link to
      	$url_alias = drupal_get_path_alias('node/'.$node->nid);

// Below are the available array keys/values we can get at
$vids[$counter]->title = $node->title;
      	$vids[$counter]->url = $base_url.$url_alias;
      	
      		if($node->thumb == ''){
      		$vids[$counter]->thumbnail = "http://cctv.dreamhosters.com/channel_17_poster.png";
      		}else{
      		$vids[$counter]->thumbnail = $this->thumbnailServer.$this->mvpath.".".$node->thumb.".tn.jpg";
      		}
      	$vids[$counter]->mvpath = $this->mvpath;
      	
      	// Increment the counter so as not to overwrite our keys
      	$counter++;
      	}
      	return $vids;
      }
      
  }  
?>

 

And here's how I'm calling the class:

 

<?php 
$rv = new RelatedVideos();
$vids = $rv->getRelatedSeries($featured_nid, $featuredSeries, 2); ?>
      <div class="related-video">
      <h3>Related Videos</h3>
	<?php foreach ($vids as $n){ ?>
            <div class="video">
                <a href="<?=$n->url;?>"><img src="<?=$n->thumbnail;?>" width="70" height="52" alt="" /></a>
                <p><a href="<?=$n->url;?>"><?=$n->title;?></a></p>
            </div> 
        <?php } ?>
    </div>

I actually finally figured this out on my own (YES!). The issue was in the parent function, I need to reset the variable, otherwise it was just appending. So my final code was this:

 

 <?php
class RootFilename {

public $RootFilename;

function __construct(){
$this->RootFilename = '';
}

function getPath($MVPath){
     // Extract root filename from Maestrovision path
     	$this->RootFilename = '';

     $this->RootFilename .= substr($MVPath, strrpos($MVPath, "\\") + 1, strrpos($MVPath, ".") - strrpos($MVPath, "\\") - 1);
     // Convert "&" to "%26" to preserve valid URLs
     $this->RootFilename .= str_replace("&", "%26", $RootFilename);
     
     return $this->RootFilename;
}
} ?>

 

Thanks to all who helped!

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.