These 2 program should give same output. What am I missing?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raul_8
Forum Newbie
Posts: 8
Joined: Mon Jun 15, 2009 11:05 am

These 2 program should give same output. What am I missing?

Post by raul_8 »

The only difference is that the 2nd program is written within a function and returns instead of echo. What is the problem?

Code: Select all

<?php
 jcarousel_add('tango');
    drupal_add_js (
      '$(document).ready(function(){
         $("#carousel-cars").jcarousel();
  }
 ); ',
      'inline'
 );
 
 echo getcwd();
 echo "<div id=\"carousel-cars-horiz\">";
 echo "<ul id=\"carousel-cars\"  class=\"jcarousel-skin-tango\">";
 
 
 
 $pic = NULL;
 $url = "./sites/all/modules/img/Images/";
 $handle = opendir($url);
 while (false !== ($file = readdir($handle))) { 
  if($file != '.' && $file != '..' && $file != 'Thumbs.db') {
  
    $str= "<li><img src =\"" . $url.$file . "\" width=90 height=80/></li>";
    
    echo $str;
  }
Following is the second program

Code: Select all

function jcrl($catname)	{

	jcarousel_add('tango');
	drupal_add_js (
      '$(document).ready(function(){
         $("#carousel-cars").jcarousel();
  }
 ); ',
      'inline'
 );
	 
	$renderjcrl =NULL;
	
	$renderjcrl = $renderjcrl . '<div id=\"carousel-cars-horiz\">';
	$renderjcrl = $renderjcrl . '<ul id=\"carousel-cars\"  class=\"jcarousel-skin-tango\">';
	 
	 
	 
	$pic = NULL;
	$url = "./sites/all/Categories/" . $catname . "/";
	
	$handle = opendir($url);
	while (false !== ($file = readdir($handle))) { 
		if($file != '.' && $file != '..' && $file != 'Thumbs.db') {
	 
			$renderjcrl =  $renderjcrl . "<li><img src =\"" . $url.$file . "\" width=90 height=80/></li>";
		}
	}
	
	$renderjcrl = $renderjcrl .  '</ul>';
	$renderjcrl = $renderjcrl .  '</div>';
	return $renderjcrl;
 
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: These 2 program should give same output. What am I missi

Post by requinix »

raul_8 wrote:The only difference is that the 2nd program is written within a function and returns instead of echo.
If that was the only difference then there wouldn't be a problem, would there?

0. First script prints the getcwd(). Sure you knew that already.
1. Different $url in both scripts.
2. Just declaring a function does not mean it will be executed (read: make sure you echo'd what the function returned).
3. The first script doesn't have the closing </ul> and </div>

There's also a disturbing lack of information about which one of those actually works and what output you receive with the other.
raul_8
Forum Newbie
Posts: 8
Joined: Mon Jun 15, 2009 11:05 am

Re: These 2 program should give same output. What am I missi

Post by raul_8 »

Ok,
0.Sorry for missing info....First code is working properly.
1.Ignore where the $url are set to.
2.Function is executig...I didn't put the entire code...just the relevant part.
3. Sorry I missed <ul> <div>

Here is the full code:

Code: Select all

<?php
    jcarousel_add('tango');
    drupal_add_js (
      '$(document).ready(function(){
         $("#carousel-cars").jcarousel();
  }
 ); ',
      'inline'
 );
 
 echo getcwd();
 echo "<div id=\"carousel-cars-horiz\">";
 echo "<ul id=\"carousel-cars\"  class=\"jcarousel-skin-tango\">";
 
 
 
 $pic = NULL;
 $url = "./sites/all/modules/img/Images/";
 $handle = opendir($url);
 while (false !== ($file = readdir($handle))) { 
  if($file != '.' && $file != '..' && $file != 'Thumbs.db') {
  
    $str= "<li><img src =\"" . $url.$file . "\" width=90 height=80/></li>";
    
    echo $str;
  }
 }
 
 echo "</ul>";
 echo "</div>";
 
?>
Post Reply