0

I have had a look within daniweb and done google searches on this problem so hopefully I am not wasting anybodys time with this. The problem I am facing is that currently I am getting the results from a mysql database using an associative array but i need to be able to compare one row of results to another. Here is the code I am using at the moment, the table has the fields pos, team, pld, won, lst, scr and con:

$result = new mysqli('localhost', 'webuser', 'userpass', 'stocker_soccer');
         if(!$result) {
		throw new Exception('Could not connect to the database server');
	} else {
		return $result;
	}

//query the database for the table data						
$query3 = "select * from league_1 order by Pos asc";
$result3 = $conn->query($query3);
$num_results = $result3->num_rows;

for($i = 0; $i < $num_results; $i++) {
   $row = $result3->fetch_assoc();

echo "<tr><td>".$row['Pos']."</td><td>".$row['Team']."</td>
      <td>".$row['Pld']."</td><td>".$row['Won']."</td><td>".$row['Drn']."</td>
      <td>".$row['Lst']."</td><td>".$row['Scr']."</td><td>".$row['Con']."</td>
      <td>".$goal_diff."</td><td>".$points."</td><td>
      <a href='table_update.php?id=".$row['id']."'>Edit Team</a></td></tr>";
}

I need to be able to compare each result row with another result row, but I do not know how to do this with what I have so far. any help with this problem would be very appreciated. I am using mysqli to access the database.

3
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by daz_omega
0

Store results of the query in one array, row per index.
Next step you want to do is create a nested for loop;
Let's say your array with row information is called $tableArray

for($i = 0; $i<count($tableArray); $i++)
{
   for($k = 0;$k<count($tableArray); $k++)
   {
        if($tableArray[$i] == $tableArray[$k])
        {
              //do something if rows are the same
        }
   }
}

This is just the skeleton of the code, in the inner for loop $tableArray[$i] stand for a single element that you are checking at that moment with entire array of others.

So what this code does, is takes one element at index [0] of the array, compares it with all other elements from that array, does what you tell it to do , and then goes to element at index [1] and so on till the end of the array.

Inside of the inner for loop (with $k) is the place where you would place your logic of comparing.

Best regards,

Toni

0

what exactly are you trying to compare. I'm more familiar with mysql but mysqli can't be that much different.
are you trying to compare each result row with what is being iterated in that set?
perhaps you just need a better query.
or could dump all your data into an array, pass the array to another function, and 'compare' it. Do you want to sort the data better, or actually compare values?

0

thanks ddymacek and tnjiric for responding. tnjiric I tried approaching the task with a nested for loop with just printing some text and this continously printed the text. I am quite new to programming, could you elaborate on why I would need a nest for loop here?

but for both of you and for anyone else it is probably best if I outline the problem better! What I am trying to do is generate a league table where the position of a team is automatically calculated. This way the client will not have to update the position field every time.

So to answer ddymacek's question I want to actually compare values, I need to compare one row of data compare the value of their points, then depending on that outcome compare their goal difference and then depending on the outcome of that compare their goals scored if that is still the same sort the two rows being compared alphabetically.

As you can see I am having problems at the first hurdle in needing to compare the values of two rows.

hopefully this helps, I will try and be more clear if this is not sufficient.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.