A simple one - IF state based on a SQL Query output

Not simple for me but im trying to change what the SQL query outputs (see below) I cant grasp if i have set a variable to represent something else why doesnt the “something else” gets echoed?

$pubvar = "";
if ($pub = "111") { $pubvar = "aaa"; }
elseif ($pub = "222") { $pubvar = "bbb";}
elseif ($pub = "333") { $pubvar = "ccc";}
else { $pubvar = "NO RECORD" ;}

while ($row = $result->fetch_object())
{
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->fullName . "</td>";
echo "<td>" . $row->contact . "</td>";
echo "<td>" . $row->address . "</td>";
echo "<td>" . $row->$pubvar . "</td>"; // <<<<< this is where im wrong, ive tried loads of variations but cant get it right
echo "<td>" . $row->total_amount . "</td>";
echo "<td>" . $row->datetime . "</td>";
echo "</tr>";
}

What you are trying to make work, dynamically pick a column, indicates a bad database design, with a series of same meaning data items stored in a single row rather than as a separate row per data item (usually in a separate related table.) This results in more complicated code/queries in order to manipulate, find, or retrieve data.

Next, if you do have a case where you are mapping an input value to an output value, don’t write out conditional logic for every possible choice. Use a look-up/map array instead, with one array element per mapping, using the input value as the array index, and the output value as the stored array value. You can then directly test if an element exists in the array (the element will be isset()) and get the output value if it exists.

Doing the above will eliminate the current problem, which is - one equal = is an assignment operator. Two == is a comparison operator. By using one = in the if() and in the elseif() conditional tests, you are assigning the value to $pub, then testing the result of the assignment, which for all of the values shown is a TRUE result, so the 1st conditional statement is always the one that gets executed.

where does $pub come from to begin with and what is it for?

It is an email address, but i want to use the if statement to say if the email address is [email protected] change to “The Bulls Head” in a query output as it just looks better than an email address. But i need the email address to be an email address in that variable for other reasons.

so, change it in the query itself

Thats what im trying to do but cant get the code right. Remember i am a beginner.

What’s the query and are you using MySQL/ MariaDB or something else?

it is mysql

if ($result = $mysqli->query("SELECT * FROM `orders` WHERE DATE(`datetime`) = CURDATE()"))

this then pulls in the the fields (s you obviously know) one being ‘pub’. I have made $pub an email address specifically so in an email form it uses the $pub from a dropdown selection form and emails the form output to that specific pub.

I am now creating a page which is a basic mysql query that displays the orders on a single page for that day (as you can see from the mysql command) but the output shows the email address, what i want to do is use an IF or SWITCH statement that if email == [email protected] then output pub name instead.

This is all for education and seeing if i can help a mate out with this virus going on for his two pubs.

So, PM me if you don’t want this public, but when I say do it in the query, I mean literally do it in the query rather than the code.

So, these values are email addresses. This is the problem with not using names for things that indicate what they are. It makes more work for you, in keeping track of everything, and anyone looking at the code cannot tell what it is doing.

This is the syntax for dynamically selecting an object property and has nothing to do with what you are actually trying to accomplish. The conditional logic you posted is before/outside of the loop, so, it would have done nothing for your current problem and implies you are doing something entirely different with the values. Had you provide some useful up front information, such as what the query was and what you were actually trying to accomplish, it would have avoided the wasted time getting to a solution.

Your data is not normalized, making everything harder to accomplish. The order information should not identify the (pub) location via its email address. You should be using a location_id, with a location table holding all the unique/one-time location information, one row per location.

The `orders` table should have columns for - id (auto-increment primary index, this produces an order id), user_id (the id of the user who placed the order), datetime (when the order was placed), location_id (the location the order was made to), ship_to_id (where the order will be shipped/delivered to), ip (the ip address of the user is typically recorded in web applications.)

The user_id would come from a user table, holding the unique/one-time user information. The location_id would come from a location table as already stated. The ship_to_id would come from an address table, holding entries related to the user_id. A returning user can pick from previously entered addresses or create a new address when placing an order.

To get any of the data ‘related’ to an order, such as the user’s real name, location information (name and email address), shipping/delivery address, … you would use a JOIN query.

Sponsor our Newsletter | Privacy Policy | Terms of Service