Solved

Passing an array into a function

Posted on 2014-03-04
3
210 Views
Last Modified: 2014-03-04
I'm using an odbc connection. I've got a "description" being listed and I want to document it using an ellipsis dynamic.

Here's the HTML:

 echo ">".$modellist[$i]["cermodelid"]." - ".$modellist[$i]["createdate"]." - ".$modellist[$i]["entityshortname"]." - ".$cer_modellistdescription($modellist[$i]["description"])."";

This is all sound as far as the values are concerned. The challenge comes in the context of calling my cermodellistdescription function, which I have coded like this:

  if (!function_exists('cer_modellistdescription')) {
    function cer_modellistdescription($modellist[$i]["description"]) {
        global $edb,$_SESSION;
        
        $sql_data =" 
        $position = 150;                    
        $message = $modellist[$i]["description"];                    
        $post = substr($message,$position,1);                                         
        if($post !=" ") {                    
        $length = strlen( $message );                    
        while($post !=" " && $position < $length){                    
        $i =1;                    
        $position = $position+$i;                    
        $message =$modellist[$i]["description"];                    
        $post = substr($message,$position,1);                             
        }                    
        };"
        if ($_SESSION["portaladmin"] == 1 && $_SESSION["showsql"] != "no")
            echo display_detail("cer/cer_functions.php",$sql_data);
        $result_data = odbc_exec($edb, $sql_data);
        $the_description=odbc_result($result_data, "$post");
        return $the_description;
         }  
    }

Open in new window


The error that I'm getting is: Parse error: syntax error, unexpected '[', expecting ')' in E:\inetpub\wwwroot\mytenbi\app\cer\cer_functions.php on line 245.

Line 245 being:  function cer_modellistdescription($modellist[$i]["description"]) {

What do I need to change?
0
Comment
Question by:brucegust
3 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 400 total points
ID: 39903576
I think you should call your function this way:

  if (!function_exists('cer_modellistdescription')) {
    function cer_modellistdescription($description) {
        global $edb,$_SESSION;
        
        $sql_data =" 
        $position = 150;                    
        $message = $description;                    
        $post = substr($message,$position,1);                                         
        if($post !=" ") {                    
        $length = strlen( $message );                    
        while($post !=" " && $position < $length){                    
        $i =1;                    
        $position = $position+$i;                    
        $message =$description;                    
        $post = substr($message,$position,1);                             
        }                    
        };"
        if ($_SESSION["portaladmin"] == 1 && $_SESSION["showsql"] != "no")
            echo display_detail("cer/cer_functions.php",$sql_data);
        $result_data = odbc_exec($edb, $sql_data);
        $the_description=odbc_result($result_data, "$post");
        return $the_description;
         }  
    }
                                  

Open in new window



and call it this way

cer_modellistdescription($modellist[$i]["description"]);
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 39903629
Agree with marqusG.  The function definition uses a placeholder variable.  PHP.net tells how to do this.
http://php.net/language.functions.php
http://php.net/manual/en/functions.user-defined.php
http://php.net/manual/en/functions.arguments.php

Do not write global $_SESSION -- it's already a global and that sort of thing is a code smell that kind of signals to the world that you don't understand variable scope.
0
 

Author Comment

by:brucegust
ID: 39903782
Thanks! This will work, but I'm wondering if I could try to do my heavy lifting on the store procedure side. If you've got some thoughts, head out to http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28379937.html.

Thanks, again!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now