Jump to content

Comparing values of 2 rows from the same table in one page


zazu
 Share

Recommended Posts

Hello guys,

 

I have a page named compare.php with two dropdown lists wich are populated with same values from a specific table.

Now what i want to do is when a user is selecting one value from the first dropdown and another value from the secound dropdown to be redirected to a page lets say compare-items.php?id=1&id=2

and here in two different cols to get the values for the items, in the first col the values for the first item, and in the secound col the values from the sec item.

 

How can i achive that? Thank you very much!

Link to comment
Share on other sites

You will have to rename one of the fields as having 2 fields called id isn't that helpful.

As for how to compare two values passed into a script - it would be better for you to attempt something and post where you are struggling rather than just ask 'how can I achieve that'!  This is something fairly trivial and should be quick to work out from almost any example using parameters.

Link to comment
Share on other sites

I've done something similar but with only one select dropdown list. The code is below

          <form action="oras.php" method="get">
              <div class="form-group">
                <?php
$result = mysqli_query($conn, "SELECT * FROM orase_disponibile ORDER BY titlu_oras ASC");
echo "<select name='id' id='id' class='form-control' required>";
echo "<option value='' disabled selected>Alegeti un oras...</option>";
while($row = mysqli_fetch_array($result))
{
  echo "<option value='" . $row['id_oras'] . "'>" . $row['titlu_oras'] . "</option>";
}
echo "</select>";
                ?>
              </div>
            <p><button type="submit" class="btn btn-primary"><i class="fa fa-search"></i> Caută dupa oras</button></p>
          </form>
Link to comment
Share on other sites

compare-items.php?id[]=1&id[]=2

 

$_GET['id'] will be an array with 2 items.

 

Loop over those 2

 

Fetch the data and echo a table for each

 

Use css to get the 2 tables to be side by side ( won't work well if any text in the table cells is multiple lines long for one id but only 1 line long for the other id )

 

Or use a more complicated html/php mixture to make a single table with all first cells from the first id and all second cells from the second id.

Edited by thinsoldier
Link to comment
Share on other sites

                <?php
$result = mysqli_query($conn, "SELECT * FROM user_pers_juridica WHERE categorie='marci'");

echo "<form action='compara-marci.php' method='get'>";
echo "<div class='form-group'>";
echo "<select name='id' id='id' class='form-control'>";
echo "<option value='' disabled selected>Alegeti un judet...</option>";
while($row = mysqli_fetch_array($result))
{
  echo "<option value='" . $row['id_companie'] . "'>" . $row['nume_companie'] . "</option>";
}
echo "</select>";
echo "</div>";
echo "<p><button type='submit' class='btn btn-primary'><i class='fa fa-search'></i> Caută</button></p>";
echo "</form>";       

$result = mysqli_query($conn, "SELECT nume_companie FROM user_pers_juridica WHERE id_companie = '$id'"); while($row = mysqli_fetch_array($result))
{
echo $row['nume_companie'];
}

?>

This is the code for the first select. Now after submit my url looks like this compara-marci.php?id=23. How can i add anouter select in this script and on submit button to show me the valus from the rows with the selected id and the url to be like compara-marci.php?id=2&id=3?

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

×
×
  • 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.