Jump to content

Recommended Posts

Hi

 

I am trying to teach myself php in my spare time; (I don't have much of it, so its a bit hit and miss).

 

I have managed to create a database and do joins and call information from the database.

 

Now I am trying to get information using an array as I want to be able to update info for a number of teams on a weekly basis.  But I can't understand the foreach loop. 

 

This is the code I'm trying, but I can't get anywhere.

 

$sql=mysql_query("SELECT teams.teamName, teams.Points, contacts.division FROM teams LEFT JOIN contacts ON teams.teamName = contacts.teamName WHERE contacts.division=1  ");


while(@$row=mysql_fetch_array($sql))
{

foreach ($teamName as $value){
   $value=$Points;
  echo $row['Points']; 
}
}

 

What I'd like it to do is print to screen the points belonging to each team.  (Ultimately, I hope to be able to update the points via a form, using foreach, if that's feasible.)

As you can probably see from the code above, I've no idea what I'm doing really.  Any help would be appreciated.  I've looked at the php site but I'm in a complete fog.

Link to comment
https://forums.phpfreaks.com/topic/112065-foreach-loop/
Share on other sites

$sql=mysql_query("SELECT teams.teamName, teams.Points, contacts.division FROM teams LEFT JOIN contacts ON teams.teamName = contacts.teamName WHERE contacts.division=1  ");

while($row=mysql_fetch_array($sql)) {
   echo "Team: " . $row['teamName'] . " Points: " . $row['Points'] . "<br />";
}

 

the mysql_fetch_array fetches the data 1 row at a time, assigning that row as an array to $row. You access each element by using the column names. Each time mysql_fetch_array is called, an internal pointer points to the next row.  When there's no more rows, the loop ends.

Link to comment
https://forums.phpfreaks.com/topic/112065-foreach-loop/#findComment-575250
Share on other sites

Hi

 

Thanks for the reply.  I know that the code you provided does what you said, but I want to be able to call the team information back into a form which will also have a drop down menu selection so that I can choose how many points to enter (from 0 -4) then have the submit button update the points information in the database.  I know that there has to be some mathematical functions in between, but I want to see if I can write back to the database, more than 1 row at a time.

 

Does this make sense?  So I thougtht I would need the foreach command to be able to do that.

 

cheers

Harlequeen

Link to comment
https://forums.phpfreaks.com/topic/112065-foreach-loop/#findComment-576063
Share on other sites

$sql=mysql_query("SELECT teams.teamName, teams.Points, contacts.division FROM teams LEFT JOIN contacts ON teams.teamName = contacts.teamName WHERE contacts.division=1  ");

while($row=mysql_fetch_array($sql)) {
   echo "Team: " . $row['teamName'] . " Points: " . $row['Points'] . "<br />";
}

 

Ideally you would have an ID field in that, eg. teamID which is an INT AUTO_INCREMENT so you can refer to the team by an ID

number rather than name.  I'd also have your teams in one table, and the score in another and join on them, that is if you want to

enter the points scored in a single game each team and then have other code to tot up the total anyway.

 

For the form/database part you need to look through your teams and output an input box for each one.

 

A useful feature of PHP is being able to name form inputs in a certain way so that you receive an array back like the following:

 

<input type="text" name="points[1]" value="" />
<input type="text" name="points[2]" value="" />
<input type="text" name="points[3]" value="" />

 

If that was put into a form and submitted you would be able to retrieve the points in PHP as an array and then you could

use foreach to get them, as you loop you would use an SQL INSERT statement to insert the points into the database.

 

            $points = $_POST['points'];
            foreach( $points as $team_id=>$score )
            {

                 // create yourself a function that INSERTS to database, and call it like the following:
                 insert_team_points( $team_id, $score );

            }

 

Your SQL statement would be something:

 

   function  insert_team_points( $team_id, $score )
   {

        // make sure we have numbers!
        $team_id = (int)$team_id;
        $score = (int)$score;

        $sql = "INSERT INTO score_table ( team_id, score ) VALUES ( $team_id, $score )";
        mysql_query( $sql ) or die ( mysql_error() . ": $sql " );

   }

 

I hope these HINTS help.   I've deliberately not supplied a full solution, because its good to learn :-)

Link to comment
https://forums.phpfreaks.com/topic/112065-foreach-loop/#findComment-576100
Share on other sites

Hi

 

Yes, I appreciate your hints.  I knew that I would have to have a form, but I thought I would be able to use this sort of code

$sql=mysql_query("SELECT teams.teamName, teams.Points, contacts.division FROM teams LEFT JOIN contacts ON teams.teamName = contacts.teamName WHERE contacts.division=1  ");


while(@$row=mysql_fetch_array($sql))
{

?>
<table width="30%" align="center"  border="1">
<form name="inputpoints" method="GET" action="teaminputtest.php">
<tr>
<td width=40%><b>
<input type="text" name="teamName" size="36" value=" <? echo $row['teamName']; ?>"></b></td>
<td><SELECT NAME=points >
<OPTION>1</OPTION>
<OPTION>2</OPTION>
<OPTION>3</OPTION>
<OPTION>4</OPTION>
<OPTION>0</OPTION><?php echo $row['Points'];?>"> 
</td>

<?php
}
?>
<input type="submit" value="submit" action="submit">
</table>

 

Using this, I have a list of team names in a table, and a drop down where I can select the number of points awarded.  Is this not possible? 

 

I understand about the team ID and I can do that. 

 

I will give this all a go tomorrow and probably be back here looking for more help... lol

 

thanks for the input though.

 

Cheers

 

Harlequeen

Link to comment
https://forums.phpfreaks.com/topic/112065-foreach-loop/#findComment-576109
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.