Jump to content

here document getting value from function


0rangeFish

Recommended Posts

I'm using a here document for the html in my php page. In it I call a function to get the correct value to pass to the form and eventually to the next php page. I'm getting an error:Array to string conversion in. I'm not sure why, maybe I'm escaping the double quotes wrong? or do you need to do something special when the value being submitted is an array?

 

This line is where the error is, the second table data in the first table row

<td><input name="choice" type="radio" value=\"getCardValues($pyramid, 0,0)\" /></td>

in here

$output =<<< _HTML_
    
    <!DOCTYPE html>

    <html>
    <head>
        <title>Pyramid Solitaire</title>
    </head>
    
    <body>
        <div class="main">
        <h1>Rock Paper Scissors Game</h1>
        
        <form action="rps.php" method="post">
            <input type="text" name="name">
            <input type="hidden" name="past" value="0">
                
            <table width="20%">
                <tr>
                    <td><img src="./images/cards/getCardFile($pyramid, 0,0)" width="100" height="100" /></td>
                    <td><input name="choice" type="radio" value=\"getCardValues($pyramid, 0,0)\" /></td>
                </tr>
                
                for($i = 0; $i < 2; $i++)
                {
                    <tr>
                        <td><img src="./images/cards/getCardFile($pyramid, 1,$i)" width="100" height="100" /></td>
                        <td><input name="choice" type="radio" value="getCardValues($pyramid, 1,$i)" /></td>
                    </tr>
                }

and so on...

 

This is the function being called:

function getCardValues($array, $row, $col)
    {        
        $valuesHolder = array($array[$row][$col].getFace(), $array[$row][$col].getSuit());
        
        return $valuesHolder;
    }
Edited by 0rangeFish
Link to comment
Share on other sites

PHP has no idea you're trying to call a function named "getCardValues". As far as it knows, there's a whole bunch of text and then suddenly $pyramid and then a lot more text after. It's not going to try to guess what you want.

 

You can use a variable and that's it. So put your getCardValues() stuff into a variable, do the same for the others, and insert those variables where you want the values to appear.

Link to comment
Share on other sites

On top of what was said above you cant place PHP code with a heredoc statement. Before the for loop you will need to close the heredoc statement and then inside the for loop you can concatenate the string to the $output variable using the concatenation assignment operator .=

 

Example

$output = <<<_HTML
...
HTML; // close heredoc statement

// start for loop
for( ... )
{
    // concatenate html to $output
    $output .= <<<_HTML
   <tr>
      ... html for row ...
   </tr>
HTML;
}

$output .= <<<_HTML
   ... rest of html for the page ...
_HTML_;

Now you may find you'll get the same error after you have done what has been suggested. This is because the getCardValues() function is returning an array. PHP wont know what you want to do with that.

 

What should be returned from that function?

Edited by Ch0cu3r
Link to comment
Share on other sites

It returns an array with the three values that are in each card: if it's not blocked by other cards, the suit, and the card value. But just returning the index is better since I can't call a function in the heredoc, then I can just pull the card from the array and get it's info that way.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.