简体   繁体   中英

Compare id from a query to id existing in another query (to see if table A id exists in table B) in a PHP echo

Summary: I am looking to compare an ID from a table to see if it exists in another table. Ideally, I would like to do this in a PHP echo statement (using the results from two separate queries). Is it possible to do it in this PHP statement, or should I set up the query to return the ids first then feed it in?

Details:

I am trying to make a php table which performs the following actions:

(1) Display results from a mysql table
(2) Allow user to add an item to a favorites list
(3) The favorites list should copy the ID from the first table to a new table
(4) Once the id exists in the other table, the icon or link next to the favorite item should change
(5) Clicking a favorite item will remove it from the table (so basically goes through the same type of process)

Right now my PHP for this page does the following:
(1) Connect to the database
(2) Query the results, loop through and build the table
(3) Pass the row id to another php page that adds it to the table

I am getting stuck as to how I can case the "favorites" icon to show it is in use and use the removal script upon a second click (I hope this makes sense)

I am new to this and not a developer (hopefully I am communicating this correctly). I'd imagine I can accomplish this by:
(1) Joining the two tables in the query and casing it
(2) Doing something else on the mysql side in the query (function etc.)
(3) Work through something in the php loop/echo

A lot of what I have read recommends Ajax, however I am trying to learn the php. Note: I already have the add and removal scripts written and working in another file, I just need help getting there.

So far I have tried

  • adding an "if" statement referencing another query in my echo (php side).
  • trying a ternary operator (php side)
  • haven't done anything specific to the mysql query yet - not sure if that would be efficient and would prefer to do from the php file to learn

No success.

/*Show grid of records for user to choose as fav, allow viewer to take action on it*/
$sql = "SELECT id, record1, record2 FROM MyTable";
$result = $cxn->query($sql);
$in = "SELECT id FROM MyFavorites"; //no idea if I should have this here or nested below
$exists = $cxn->query($in);
echo "<table><tr><th>record1</th><th>record2</th></tr>";
if ($result->num_rows > 0) { 
// output data of each row
while($row = $result->fetch_assoc()) {
//bring back records from table and attempt to case rowid to see if it exists in favorites table
    echo "<tr><td> " . $row["id"]. " " . $row["record1"]. "</td><td> " . $row["record2"]. "</td><td><a href='AddFavorite.php?favid=".$row['id']."'>Add</a></td><td>($row["id"]=$in)? "active":""</td></tr>";
} //basically what I want here is if exists direct to remove.php, if not exists redirect to add.php - adjust text accordingly

} else {
echo "no results";
}
echo "</table>";

Don't know if I understand you well, but as I read the code I found the following:

When you are doing $cxn->query(...) you receive a PdoStatement . Not explicitly a result from the database.

So, in your code I see: "...<td>($row["id"]=$in)? "active":""</td>..."

And that must be a compilation error.

I think you are trying to use ternary operator: (condition) ? true-part : false-part (condition) ? true-part : false-part

Then it should looks like:

"...<td>" . (($row["id"] == $in) ? "active" : "") . "</td>..."

Anyhow, $in is a string containing "SELECT id FROM MyFavorites" and comparing $row["id"] to that string will return false.

You could query the data including if it exists or not inside the table MyFavorites , something like:

SELECT t.id, record1, record2, IF(f.id IS NULL, 0, 1) as exist_on_favorites FROM MyTable AS t LEFT OUTER JOIN MyFavorites f ON (t.id = f.id)

  • FROM MyTable AS t LEFT OUTER JOIN MyFavorites f ON (t.id = f.id) means: all records from table MyTable, lets call it t, and also those records from table MyFavorites, lets call it f, where t.id equals f.id.
  • IF(f.id IS NULL, 0, 1) : If f.id is null because there was no related id in that table, return 0, else return 1. That value will be in the field exist_on_favorites.
  • Now you can "...<td>" . (($row["exist_on_favorites"] == 1) ? "active" : "") . "</td>..." "...<td>" . (($row["exist_on_favorites"] == 1) ? "active" : "") . "</td>..."

Please, read more about relational databases, relations one-to-one, one-to-many, many-to-many.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM