Results 1 to 3 of 3

Thread: Finding record in array

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Finding record in array

    So I have a drop down box that displays all the entries in a column or a multidimensional array.
    I want to be able to select one of the Teams (this is football related) from the drop down menu, and it show me the rows in the array where that team is selected.
    my code:
    Code:
    <?php
    	if(isset($_POST['formSubmit'])) 
    	{
    		$aTeams = $_POST['formTeams'];
    		
    		if(!isset($aTeams)) 
    		{
    			echo("<p>You didn't select a Team!</p>\n");
    		} 
    		else 
    		{
    			$nTeams = count($aTeams);
    			
    			echo("<p>You selected Team ");
    			
    				echo($aTeams . ", ");
    
    			echo("</p>");
    }
    		
    	}
    ?>
    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    	<label for='formTeams'>Select the first team</label><br>
    
    <select name="formTeams[]">
        <option value="0">Choose a Team</option>
        <?php showOptionsDrop($csvData, 'HomeTeam', null, true); ?>
    </select>
    <input type="submit" name="formSubmit" value="Submit" />
    showOptionsDrop is a function that successfully populates the drop down box with the names of the teams in the column with the header HomeTeam.
    The php code above is supposed to say you selected Team ...... however it always says You Selected Team Array?

    How do I get it to show the name of the entry in the array csvData? And then display in a table all the entrys in the array where they are the HomeTeam?
    Im quite new to php, Im getting on alright, just a couple of things that I havent completely got yet.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Finding record in array

    Well, this sure makes more sense than that other post I replied to!

    $_POST['formTeams'] is a form array, so printing $aTeams will always return the text "Array." PHP can't print out an array without some help, and the only way it can is not in a way useful for you, especially not for what you're trying to do.

    First of all, let's break down what you're doing wrong. Here:
    PHP Code:
    if(isset($_POST['formSubmit'])) 
        {
            
    $aTeams $_POST['formTeams'];
            
            if(!isset(
    $aTeams)) 
            {
                echo(
    "<p>You didn't select a Team!</p>\n");
            } 
            else 
            {
                      
    // .. removed
                    
    }
        }
    ?> 
    You check if $_POST['formTeams'] is set, and if it is, you assign $aTeams to it. Then you check if $aTeams is set. Well, your logic says that to get to this point at all $_POST['formTeams'] must be set, meaning that $aTeams will always be set because $aTeams holds $_POST['formTeams']. So, this if statement will never go into the first condition. You should structure it like below if you want to display an error message of some kind:
    PHP Code:
    if(isset($_POST['formSubmit'])) 
        {
            
    $aTeams $_POST['formTeams'];
            
                    
    // .. removed
        
    }else{
                    echo 
    "<p>You didn't select a Team!</p>\n";
            }
    ?> 
    Much cleaner, and will actually provide what you want. However, this will also make the page always display your error message, even when they first visit the page and haven't had a chance to select a team yet. This is undesirable. So, check to make sure they actually submitted the form before doing any of this processing:

    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == "POST"){

        if(isset(
    $_POST['formSubmit'])) 
        {
            
    $aTeams $_POST['formTeams'];
            
                    
    // .. removed
        
    }else{
                    echo 
    "<p>You didn't select a Team!</p>\n";
            }

    }
    ?> 
    Here, we check if the server's REQUEST_METHOD is set to POST, and if it is, then we do our processing. This ensures that at the very least, we have a POST request and the user has done something. The error message won't be displayed on the first page load, only after the form is submitted.

    Now, onto your actual problem. You want to display some information from your $csvData array. I'm going to assume that showOptionsDrop() will print the index of into the option's "value" attribute, but if you're uncertain about this then the source for showOptionsDrop() will need to be looked at. But if that function is all good, then we can move ahead.

    Do you know for certain that you want formTeams[] to be an array? Is there going to be a home team as well as an away team? If so, then your first team will be stored in $_POST['formTeams'][0] and your second in $_POST['formTeams'][1] (which does not yet exist on your form). You can probably get the CSV data by accessing:

    PHP Code:
    print_r($csvData[$_POST['formTeams'][0]]); 
    Or, more easily:

    PHP Code:
    $teamIndex $_POST['formTeams'][0];
    print_r($csvData[$teamIndex]); 
    Make any sense?
    Like Archer? Check out some Sterling Archer quotes.

  3. #3
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Finding record in array

    OP, instead of making new threads on the same code (that omit relevant context like function definitions), it'd be helpful if you just posted new questions in your original thread.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width