Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 06-25-2006, 08:19 PM Writing a function
NPL Solutions's Avatar
Skilled Talker

Posts: 66
Location: Colorado (USA)
Trades: 0
Hey.

I feel completely idiotic asking this, but are there any special conditions that need to be taken into account when writing functions with PHP? I took the following code right out of the center of my script (except for the echo "Test#"), so it should be all correct, but when I try to execute the function, all it does is echo the "Test_end".

PHP Code:
function newProd(){
    
$new_prod false;
    
$new_prod_heading false;
    
$loop_break_new false;
    
    for(
$np=0;$np<count($product_array);$np++){
        echo 
"Test1";
        if(!
$loop_break_new){
            while(
$product_array[$np]->cat != $cat_id){
                
$np++;
            }
            if(
$product_array[$np]->cat == $cat_id){
                echo 
"Test2";
                if(
$product_array[$np]->age == 1){
                    echo 
"Test3";
                    
$new_prod true;
                    if(!
$new_prod_heading){
                        echo 
"Test4";
                        echo 
"<p class=\"title_newprod\">New Products</p>
                        <hr class=\"new_prod\" />"
;
                        
$new_prod_heading true;
                    }
                    
// Output prod_row
                    
echo "<div class=\"prod_row\">";
                    
$new_prod_div true;
                                    
                    
// Output prod_box's
                    
for($p=0$p<5$p++){
                        echo 
"Test5";
                        if(
$p == && $product_array[$np-1]->cat == $cat_id){
                            
$np--;
                        }
                        
// Checks to see if there is a category, and limits the output to that category
                        
if($product_array[$np]->cat == $cat_id){
                            echo 
"Test6";
                            if(
$np<count($product_array) && $product_array[$np]->age == 1){
                                echo 
"Test7";
                                
// Code for each of the prod_box's
                                
echo "<div class=\"prod_box\"> 
                                    <div class=\"prod_image\">
                                    <a href=\"item.php?item_id=" 
$product_array[$np]->id ."\">";
                                        if(
file_exists($product_array[$np]->image)){
                                            
// Product image generation (notice syntax)
                                            
echo "<img src=\"image.php?image_name=".$product_array[$np]->image."&amp;image_height=100&amp;image_width=110\" height=\"100\" alt=\"" $product_array[$np]->name " - " $product_array[$np]->id ."\" />";
                                            }
                                        else{
                                            
// Product placeholder generation (notice syntax)
                                            
echo "<img src=\"image.php?image_name=images/No_image.jpg&amp;image_height=100&amp;image_width=110\" height=\"100\" alt=\"" $product_array[$np]->name ." - " $product_array[$np]->id "\" />";
                                            }
                                    echo 
"</a></div>
                                    <div class=\"prod_text\">
                                    <a href=\"item.php?item_id=" 
$product_array[$np]->id ."\">
                                        " 
$product_array[$np]->name "<br/></a>";
                                        echo 
"
                                   </div>
                                </div>"
;
                            }
                        }
                        
$np $np $product_array[$np]->variations;
                        if(
$product_array[$np]->cat != $cat_id && $product_array[$np]->group != $group_id){
                            
$loop_break_new true;
                            
$p 4;
                        }
                    }
                    echo 
"</div>";
                }
            }
        }
    }    
    echo 
"Test_end";

What am I missing?

Thanks.

Nicky
NPL Solutions is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-25-2006, 08:30 PM Re: Writing a function
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
Where is $product_array coming from? You are count()'ing it as part of your for loop condition, but there is no $product_array in the scope of the function.

Make sure to read about variable scope.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 06-25-2006, 08:43 PM Re: Writing a function
NPL Solutions's Avatar
Skilled Talker

Posts: 66
Location: Colorado (USA)
Trades: 0
$product_array is included from a separate file, I could post the entirity of both the scripts, but there's a lot of code that I don't think would mean too much. What confuses me is how only the last line is actually displayed when the function executes.

Nicky
NPL Solutions is offline
Reply With Quote
View Public Profile
 
Old 06-25-2006, 09:23 PM Re: Writing a function
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
All vars outside of a function will not be made available to the function unless passed through the () of the function or declared global inside the function (unless the var is a super global).
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 06-26-2006, 01:39 AM Re: Writing a function
NPL Solutions's Avatar
Skilled Talker

Posts: 66
Location: Colorado (USA)
Trades: 0
Thank you SO much! I'm a little perplexed as to why this is true, but hey, as long as I know what's up, that's what counts. This makes my life so much easier.

Thank you!

Nicky
NPL Solutions is offline
Reply With Quote
View Public Profile
 
Old 06-26-2006, 04:26 PM Re: Writing a function
Charley Delta's Avatar
Experienced Talker

Posts: 32
Location: Rochester, NY USA
Trades: 0
Nicky, it works that way because once upon a time, like when I learned to program in BASIC back in 1981, it was all too easy to accidentally change the value of a variable from anywhere in the program. Imagine having to step through hundreds of lines of code to find that one little typo that threw everything off. Having come from a place where there were no functions or variable scope (we used GOTOs, and every variable was global), this way makes it so much easier to work.
__________________
Always take into account what nobody would ever do in a million years, because someone will.
Charley Delta is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Writing a function
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.20237 seconds with 12 queries