Miscellaneous > Programming & Networking

php debugging

(1/1)

worker201:
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: ---if (count($input1) = 1) {
--- End code ---

And here's the whole code, for context (the line above is found in function_buildform3):

--- Code: ---<?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;
}
?>
--- End code ---

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.

piratePenguin:
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)

worker201:
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:
postgresql is a damn good database

worker201:
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: ---<?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;
}
?>
--- End code ---

Navigation

[0] Message Index

Go to full version