Author Topic: php debugging  (Read 1723 times)

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
php debugging
« on: 11 February 2010, 16:48 »
I need some help, and I don't know where else to turn.  3 years ago, there were a handful of PHP coding forums that offered plenty of help.  But for whatever reason (PHP is out of favor and so are forums?), they aren't active anymore.

So the error I'm getting is "Fatal error: Can't use function return value in write context in /Users/lholcombe/Sites/triple-bypass/process3.php on line 84".  Which I'm sure is related to the function reference variables I am passing.  I didn't want to use things like reference variables specifically because I knew it would fuck things up.  But I didn't know how else to get the effect I wanted.

Any assistance would be greatly appreciated.

The line in question is:
Code: [Select]
if (count($input1) = 1) {
And here's the whole code, for context (the line above is found in function_buildform3):
Code: [Select]
<?php
$prefix1 
$_POST['prefix1'];
$name1 $_POST['name1'];
$type1 $_POST['type1'];
$suffix1 $_POST['suffix1'];

$prefix2 $_POST['prefix2'];
$name2 $_POST['name2'];
$type2 $_POST['type2'];
$suffix2 $_POST['suffix2'];

$success 1;
$output 1;
street_process($prefix1,$name1,$type1,$suffix1,1,$success,$output);
if (
$success == 0) {
    
not_found(1);
}
    else {
        
$output1=$ouput;
    }

$success 1;
$output 1;
street_process($prefix2,$name2,$type2,$suffix2,2,$success,$output);
if (
$success == 0) {
    
not_found(2);
}
    else {
        
$output2=$ouput;
    }

build_form3($output1,$output2);
// End main function

// function street_process
function street_process($prefix,$name,$type,$suffix,$iteration,&$success,&$output)
{
    
$name strtoupper($name);
    
$conn_string "host=localhost port=5432 dbname=testKCstreets user=postgres password=*******";
    
$db pg_connect($conn_string);
    
$query="SELECT fullname FROM streets.roadtest WHERE st_name='$name'";
    if (
$prefix != "0") {
        
$query .= " AND dirprefix='$prefix'";
    }
    if (
$suffix != "0") {
        
$query .= " AND dirsuffix='$suffix'";
    }
    if (
$type != "0") {
        
$query .= " AND st_type='$type'";
    }
    
$result pg_query($db$query);
    if (!
$result) {
      echo 
"An error occured.\n";
      exit;
    }
    for (
$i=0$i<pg_numrows($result); $i++){
        
$result_list[$i] = pg_fetch_result($result$i"fullname");
    }
    if (
is_null($result_list[0])) {
        return 
$success 0;
    }
        else {
            
$output array_unique($result_list);
            
sort($output);
            return;
            }
}

//function not_found
function not_found($iteration)
{
    echo 
"Street number $iteration was not found.<br>";
    echo 
"Please make sure the street you are searching for is in King County.<br>";
    echo 
"Also, please check your spelling.<br>";
    echo 
"Click the button below to return to the previous page.<br>";
    echo 
"<form name=\"form2\" action=\"street_intersect.htm\"><input type=\"button\" value=\"Return\"></form>";
    exit;
}

// function build_form3
function build_form3($input1,$input2)
{
    echo 
"<form name=\"form3\" action=\"intersect.php\" method=\"post\">";
    if (
count($input1) = 1) {
        echo 
"Street number 1 is:<br>";
        echo 
$input1[0];
    }
        else {
            echo 
"Multiple results were found for street number 1.  Please select one:<br>"
            
for ($i=0$i<count($input1); $i++){
                echo 
"<input type=\"radio\" name=\"street1"$i"\" value=\""$input1[$i], "\">";
                echo 
$input1[$i];
                echo 
"<br>";
            }
    }
    echo 
"<br><br>";
    
    if (
count($input2) = 1) {
        echo 
"Street number 2 is:<br>";
        echo 
$input2[0];
    }
    else {
        echo 
"Multiple results were found for street number 2.  Please select one:<br>"
        
for ($i=0$i<count($input2); $i++){
            echo 
"<input type=\"radio\" name=\"street2"$i"\" value=\""$input2[$i], "\">";
            echo 
$input2[$i];
            echo 
"<br>";
        }
    }
    echo 
"<br><br>";
    
    echo 
"If these values are correct, click <i>Submit</i>.<br>";
    echo 
"Otherwise, click <i>Return</i> to try again.<br>";
    echo 
"<input type=\"submit\" name=\"submit\" value=\"submit\">";
    echo 
"<input type=\"button\" name=\"return\" value=\"Return\" a href=\"street_intersect.htm\">";
    echo 
"</form>";
    exit;
}
?>

For the record, I'm 100% self taught in PHP.  I did some modifications in phpBB a few years ago, but this is my first time trying to build something from the ground up.  Pretty much everything I know comes from the PHP.net documentation, which I found last week.
« Last Edit: 11 February 2010, 16:52 by worker201 »

piratePenguin

  • VIP
  • Member
  • ***
  • Posts: 3,027
  • Kudos: 775
    • http://piratepenguin.is-a-geek.com/~declan/
Re: php debugging
« Reply #1 on: 11 February 2010, 18:02 »
if (count($input1) = 1) {

should be ==
!!
Now the error message makes some sorta sense :) i think

btw I used to know php, its a pretty fucked up language really. It's simple but in order to build REALLY meaningful applications you need a good framework, there are php frameworks but I decided to learn python (first django but then Pylons), I think it was the best move I could make.

PHP has an extremely bad rep but mostly thats because most people who write php write very disorganized code or write applications with no good structure because the language allows that sorta stuff. PHP is very easy, but that makes it very easy to program badly with it too. If you just want a specific page or a simple site, it can be 100% though.

(This is what I've gathered from what people are saying, I dont have any modern or very meaningful experience with php)
"What you share with the world is what it keeps of you."
 - Noah And The Whale: Give a little love



a poem by my computer, Macintosh Vigilante
Macintosh amends a damned around the requested typewriter. Macintosh urges a scarce design. Macintosh postulates an autobiography. Macintosh tolls the solo variant. Why does a winter audience delay macintosh? The maker tosses macintosh. Beneath female suffers a double scum. How will a rat cube the heavier cricket? Macintosh calls a method. Can macintosh nest opposite the headache? Macintosh ties the wrong fairy. When can macintosh stem the land gang? Female aborts underneath macintosh. Inside macintosh waffles female. Next to macintosh worries a well.

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: php debugging
« Reply #2 on: 11 February 2010, 23:12 »
Awesome, that did it.  That one messes me up every single time.

I'm mostly using PHP as part of a LAMP environment.  Or, in this case, a MAPP environment - Mac, Apache, PostgreSQL, PHP.  It's easy to use, easy to maintain, and you can get started pretty quickly.  For free.  I'm using PostgreSQL instead of MySQL because I'm interested in the spatial geometry properties that PostgreSQL supports (through the PostGIS extension).

piratePenguin

  • VIP
  • Member
  • ***
  • Posts: 3,027
  • Kudos: 775
    • http://piratepenguin.is-a-geek.com/~declan/
Re: php debugging
« Reply #3 on: 12 February 2010, 08:05 »
postgresql is a damn good database
"What you share with the world is what it keeps of you."
 - Noah And The Whale: Give a little love



a poem by my computer, Macintosh Vigilante
Macintosh amends a damned around the requested typewriter. Macintosh urges a scarce design. Macintosh postulates an autobiography. Macintosh tolls the solo variant. Why does a winter audience delay macintosh? The maker tosses macintosh. Beneath female suffers a double scum. How will a rat cube the heavier cricket? Macintosh calls a method. Can macintosh nest opposite the headache? Macintosh ties the wrong fairy. When can macintosh stem the land gang? Female aborts underneath macintosh. Inside macintosh waffles female. Next to macintosh worries a well.

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: php debugging
« Reply #4 on: 12 February 2010, 10:50 »
Totally rebuilt and commented the code.  I knew that nonsense with globals and references was no good.  I didn't realize you could just assign a return value to a variable - once that clicked, I could kill off like 10 lines.  Which is totally something I would learn in a class, but might never come across in a cookbook. 

So everything is streamlined, and I think it works with every possible case I can think of.
(those boxes around the function definitions look better in my terminal font).
Code: [Select]
<?php
////////////////////////
// begin main function//
////////////////////////

// post variable declarations from street_intersect.htm
$prefix1 $_POST['prefix1'];  // directional prefix
$name1 $_POST['name1'];
$type1 $_POST['type1'];
$suffix1 $_POST['suffix1']; // directional suffix

$prefix2 $_POST['prefix2'];
$name2 $_POST['name2'];
$type2 $_POST['type2'];
$suffix2 $_POST['suffix2'];

$output1 street_process($prefix1,$name1,$type1,$suffix1); // try to match the first street
if ($output1 == 0) { // no match of street name found
    
not_found(1);
}
$output2 street_process($prefix2,$name2,$type2,$suffix2); // try to match the second street
if ($output2 == 0) {
    
not_found(2);
}

build_form3($output1,1); // build the form
build_form3($output2,2);
finalize_form();

///////////////////////
// End main function //
///////////////////////

////////////////////////////////////////////////
// function street_process                    //
// builds the query and performs the query    //
// returns zero if no match is found          //
// otherwise returns array of possible matches//
////////////////////////////////////////////////
function street_process($prefix,$name,$type,$suffix)
{
    
$name strtoupper($name);
    
$conn_string "host=localhost port=5432 dbname=testKCstreets user=postgres password=******";
    
$db pg_connect($conn_string);
    
$query="SELECT fullname FROM streets.roadtest WHERE st_name='$name'";
    if (
$prefix != "0") {
        
$query .= " AND dirprefix='$prefix'";
    }
    if (
$suffix != "0") {
        
$query .= " AND dirsuffix='$suffix'";
    }
    if (
$type != "0") {
        
$query .= " AND st_type='$type'";
    }
    
$result pg_query($db$query);
    if (!
$result) { // could not connect to database
      
echo "An error occured.\n";
      exit;
    }
    for (
$i=0$i pg_numrows($result); $i++){
        
$result_list[$i] = pg_fetch_result($result$i"fullname"); //only get fullname, not interested in spatial geometry at this time
    
}
    if (
is_null($result_list[0])) { // no match of street name found
        
return 0;
    }
    else {
        
$output array_unique($result_list); // get rid of multiple instances
        
sort($output); // reindex array after unique
        
return $output;
        }
}

////////////////////////////////////////////////////////////////////////
// function not_found                                                 //
// called if street_process returns no matches                        //
// terminates script and allows user to return to street_intersect.htm//
////////////////////////////////////////////////////////////////////////
function not_found($iteration)
{
    echo 
"Street number $iteration was not found.<br>";
    echo 
"Please make sure the street you are searching for is in King County.<br>";
    echo 
"Also, please check your spelling.<br>";
    echo 
"Click the button below to return to the previous page.<br>";
    echo 
"<form name=\"form2\" action=\"\"><input type=\"button\" value=\"Return\" onClick=\"window.location='street_intersect.htm'\"></form>";
    exit;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// function build_form3                                                                                  //
// creates the query confirmation form                                                                   //
// if a street has multiple matches, user is instructed to choose which one s/he wants to use            //
// if a street has only one match, show it to the user                                                   //
// if there are no matches, the script should have terminated already, but we'll check again just in case//
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function build_form3($input,$iteration)
{
    echo 
"<form name=\"form3\" action=\"intersect.php\" method=\"post\">";
    if (
count($input) < 1) { // if this happens, script should have already terminated, so this is here just to watch variables
        
echo "An unknown error has occurred!  Terminating.";
        exit;
    }
    elseif (
count($input) == 1) { // query returned exactly 1 match
        
echo "Street number $iteration is:<br>";
        echo 
"<input type=\"hidden\" name=\"street"$iteration"\" value=\""$input[0], "\">"// hidden text box
        
echo $input[0];
    }
    else {  
//implies count is greater than 1, multiple query matches
        
echo "Multiple results were found for street number $iteration.  Please select one:<br>";
        for (
$i=0$i count($input1); $i++){
            echo 
"<input type=\"radio\" name=\"street"$iteration"\" value=\""$input[$i], "\">"// radio buttons
            
echo $input[$i];
            echo 
"<br>";
        }
    }
    echo 
"<br><br>";
}

/////////////////////////////////////////////////
// function finalize_form                      //
// writes submit and return buttons to the form//
// doesn't accept or return arguments          //
/////////////////////////////////////////////////
function finalize_form()
{
    echo 
"If these values are correct, click <i>Submit</i>.<br>";
    echo 
"Otherwise, click <i>Return</i> to try again.<br>";
    echo 
"<input type=\"submit\" name=\"submit\" value=\"submit\">";
    echo 
"<input type=\"button\" name=\"return\" value=\"Return\" onClick=\"window.location='street_intersect.htm'\">";
    echo 
"</form>";
    exit;
}
?>