Solved

Passing an array into a function

Posted on 2014-03-04
3
207 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 30

Accepted Solution

by:
Marco Gasi earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

7 Experts available now in Live!

Get 1:1 Help Now