Combine PHP foreach loop with a SQL foreach loop - php

Forward:
I've checked through the similarly worded questions on Stack Overflow but none are specific to what I'm attempting.
What I have:
I have session data, two loops and a SQL table.
The first loop checks for specific existing session data and uses
the keys (rather than the values) of this data to determine what ids
should be selected (SQL) from a table.
The second loop outputs the SQL results row by row.
...this can also be described in a less abstract and friendlier manner:
The first loop looks for items currently added to the shopping cart
(key = product id, value = quantity) to determine what products
should be listed on "Your order" page.
The second loop list rows of data associated with each product id
such as the title, description and unit cost.
What I need:
Since my session data consists of products ids (keys) and a corresponding quantity (values) I need to show the quantity as part of each outputted row.
id | title | description | unit cost | amount | subtotal
session data key | from database | from database | from database | session data value | basic PHP math
(also in database) (unit cost * amount)
My code:
The first loop to use the session data keys to query the require products:
$id_from_session = "WHERE id = '9999999999'"; //fix due to not being sure how to simuataneously incorporate "WHERE" and "OR" into query loop (only looping the "OR" condition).
foreach($_SESSION['post'] as $key=>$value)
{
$id_from_session .= "OR id ='".$key."'";
}
The second loop to output the resulting rows of the query:
foreach ( $yourorders as $yourorder )
{
?>
<?php echo $yourorder->title; ?></br>
<?php echo $yourorder->description; ?></br>
<?php echo $yourorder->value; ?></br>
<?php /* I NEED THE QUANTITY HERE */ ?></br>
<?php /* VALUE * QUANTITY HERE */ ?></br>
<?php
}
<?php /* OVERALL TOTAL OUTSIDE OF LOOP */ ?></br>
My question:
How do I incorporate the quantity from the session data loop (and the quantity * unit value) into the SQL results loop?

$_SESSION['post'][$yourorder->id]
should get you the quantity right? so,
<?php echo $_SESSION['post'][$yourorder->id]; ?>
or if short tags are allowed
<?= $_SESSION['post'][$yourorder->id]; ?>
Also a nicer way to build the WHERE string.
$where = array();
foreach($_SESSION['post'] as $key=>$value)
{
$where[] = "id ='".$key."'";
}
$id_from_session = "WHERE " . implode(' OR ', $where);

$_SESSION['post'][$yourorder->id] gives you the quantity since the keys are the product ids :)
well.. if you can be absolutely sure that your shopping cart keys don't need to be escaped you can write this:
$id_from_session = "WHERE id IN ('" . implode( "', '", array_keys($_SESSION['post']) ) . "')";

Related

My cart items aren't printing in the same order in which they're placed

My code isn't printing the products in the order I placed them into the cart session. The product ids are as follow:
Apples value of 2
Blackberries value of 3
Green grapes value of 6
As you can see in the printed array, the order in which I placed them into the cart session is Grapes, Blackberries and then Apples but for some reason it's printing in the order of their given values rather than their cart order.
The code still treats each of the products as if they're in the correct place however. For example, Apples are suppose to be the third item in the cart array where the grapes currently are. If the quantity button of the grapes is changed, the quantity of Apples would be changed instead because it's still reading the array correctly.
I suspect the problems lie in one of the loops but I just can't seem to find what it is.
<?php
$total = 0;
$id_count = 0;
if(isset($_SESSION['cart']) and count($_SESSION['cart']) > 0){
$product_ID = array_column($_SESSION['cart'],"product_ID");
$result = $database->getData();
while($row = mysqli_fetch_assoc($result)){
foreach($product_ID as $id){
if($row['id']==$id){
print_r($product_ID);
print_r($row['product_name:']);
echo " value: $id";
$itemQuantity = $_SESSION['cart'][$id_count]['quantity'];
cartMethod($row['product_name'],$row['product_img'],$row['product_price'], $row['id'], $itemQuantity);
$total = $total + ($itemQuantity*(float)$row['product_price']);
$id_count++;
}
}
}
}
else{
echo "<h5>Cart is currently empty</h5>";
}
?>
This is the getData() function located in a different file.
//get method to retrieve database information
public function getData(){
$sql = "SELECT * FROM $this->tablename";
$result = mysqli_query($this->connection, $sql);
if(mysqli_num_rows($result)>0){
return $result;
}
}
You're iterating over the product catalog from the database ($database->getData()), and then for each entry in there crosschecking if it exists in $product_ID.
So the effective order follows that of the query (which is probably ORDER BY id or something i suspect (i can't tell; the code isnt included).
It's not hard to fix but you are building the cart in a very inefficient method here. It would make a lot more sense to do the bulk of data collation (which is what most of this page is), inside the database.
Of course, i don't know what the product catalog looks like in the database, and i don't know what query hides behind $database->getData(), so i cannot really give more targetted advice.
If i had to guess, i'd suspect that $database->getData() contains a query such as SELECT * FROM products ORDER BY productid ASC. If that is indeed the case then i would extend that query so you can feed the $_SESSION['cart'] data into the SELECT query, and join that against the products table.
That way:
A) you have full control over the ordering
B) you only have to return the relevant products and not "all" products, muchhh more efficient.
C) you dont have to mix&match the 2 datasets in PHP

PHP displays only the first value in a for() loop insinde another foreach() loop

I have small issue with a for() loop that displays values inside another foreach() loop in a dynamic way.
A small background of the code:
The output table is created dynamically based on the values obtain from one mysql query SELECT (which is the 1st select) from 1 column (that has name "coloane") in a table called "categories";
The values stored in the column "coloane" are actually the name of some columns from another table called "articles"
The first mysql select is to obtain the values from the column "coloane" so I can create the second.
After the second table returns rows (num_rows() > 0) I start to create the table.
The table has 2 static columns that are always present, and the rest of the columns are added dynamically based on different criteria.
The plot:
I explode the values obtained from the first select - from the column "coloane" - to make the [th]'s for the table header;
this explode I store it under the variable "$rows_th";
to make the [th]'s for the table header, i use a foreach() loop (and here I also remove any underscores);
after that I proceed to create the [tbody] and the [td]'s using another foreach(), only that this foreach is done for the values of the second select;
inside this foreach() I create another loop, using the for() function on the variable "$rows_th" so the table has the same length;
here the values are combined like this:
'<td id="'.$i.'">'.$values_td[$rows_th[$i]].'</td>';
The main issue is that, using the format listed above, it will only display the first value!
When I tried to verify the "$values_td[$rows_th[$i]]" using the is_string() function, I discovered that the first value is a string, and after that, every other value is not a string...
The test performed:
if (is_string($values_td[$rows_th[$i]]))
print '<td id="'.$i.'">Value found!!!</td>';
} else {
print '<td id="'.$i.'">No luck...</td>';
}
Any ideas on what might be going wrong ?
I know I'm missing something, but I just can't seem to figure it out :-/
The entire code is listed here: http://pastebin.com/4MFifh92
In the mean time, with the help from a friend, I finally found what was going wrong.
The variable turns from string to number because in the table called "categories", the column named "coloane" has the values inside devided by a "," (comma) and a " " (space), like so
------------------------------------------
| coloane |
------------------------------------------
make, model, location, comments, status
------------------------------------------
To fix this, I found out that I can try to do a str_replace in a simple foreach() loop :)
Something like this:
$i=0;
foreach($rows_th as $values_th) {
$i++;
$cols_td = str_replace(' ', '', $values_th);
$actual_value = $values_td->$cols_td;
print '<td id="'.$i.'">'.$actual_value.'</td>';
}
And this did the trick :)
Hope this will help others that came across with issues similar to this :)
Best regards,
Mike
Can't you just do something like:
SELECT * FROM results_query_header_by_cat WHERE `id` = 3;
Then:
SELECT * FROM results_query_content_by_cat WHERE `id_category` = 3;
Then with the results:
echo '[thead]';
foreach ( $results as $result ){
foreach( $result['coloane'] as $header ){
echo '[th]'.$header.'[/th]';
}
}
echo '[/thead][tbody]';
foreach ( $records as $record ){
echo '[tr][td]' .
$result['producator'] . '[/td][td]' .
$result['model'] . '[/td][td]' .
$result['imei'] . '[/td][td]' .
$result['locatie'] . '[/td][/tr]';
}
echo '[/tbody]';
Is that what you're looking for?
e: I haven't tried this but I'd imagine it's something like that. Just:
get the headers by category ID
get the records by category ID.
Echo the first result set as headers with a loop,
echo the second result set as table rows with columns in a loop.

Query 2 tables and combine data in a single array

I am trying to combine the results from 2 queries into a single array but am having problems getting the correct code.
I have a table called PrimaryEvents that I query to add to an array. This query works ok:
$rows = query("SELECT * FROM PrimaryEvents WHERE unit = ? ORDER BY event", $unit);
I add the data to an array using the following:
foreach ($rows as $row)
{
// $event = lookup($row["event"]); (removed as spotted by sergio not required)
$event["event"] = $row["event"];
$event["validity"] = $row["validity"];
$event["eventsrequired"] = $row["eventsrequired"];
$event["unit"] = $row["unit"];
$event["role"] = $row["role"];
//now we have the data lets try and do something to it
if ($event["role"] == "0")
{
//then the user is active so change value to a YES
$event["role"] = "ALL";
}
//add our stock to the portfolio array
$portfolio[] = $event;
}
So far everything works well. I would like to query a separate table and add certain results to the array portfolio[];
$rowsCompleted = query("SELECT * FROM portfolio WHERE id = ? ORDER BY name", $_SESSION["id"]);
My original plan was to use the following additional code to add the data of interest to the portfolio[] but it doesn't work:
//now add individual stats to the array
foreach($rowsCompleted as $row)
{
if ($portfolio["event"] == $row["name"]
{
//then add our number and date to the array
$portfolio["datecompleted"] = $row["datecompleted"];
$portfolio["number"] = $row["number"];
}
//else do nothing
}
My final aim was to pull the datacompleted value and number value from the portfolio table and then add it to the array ONLY if the event name matches the name in portfolio array.
Hopefully I have described my problem and required behaviour well enough.
Thanks for nay help that can be offered, I am new to php/sql so learning as I go.
Andy
EDIT:
Also tried the following loop to try and loop through the array:
//now add individual stats to the array
foreach($rowsCompleted as $row)
{
foreach ($portfolio["event"] as $item)
{
if ($portfolio["event"] == $row["name"]
{
//then add our number and date to the array
$portfolio["datecompleted"] = $row["datecompleted"];
$portfolio["number"] = $row["number"];
}
}
//else do nothing
}
EDIT2: To try and make my question more clear: The Primary-events table lists all the events a specific user has to complete. The portfolio table tracks which events have been completed by the user (by means of date-completed and number).
I want to run a query that lists all the events from Primary-events, then into those results add the date-completed and number from the users portfolio. If the answer is blank from the portfolio then the user has not yet completed the event but I would still like to list it from the Primary-events table data.
I tried 2 queries to start with as it seemed to follow what I was trying to achieve.
EDIT3:
New code with help from Barmar
foreach ($rows as $row) {
$event = lookup($row["event"]);
$event["event"] = $row["event"];
$event["validity"] = $row["validity"];
// $event["datecompleted"] = $row["datecompleted"];
// $event["number"] = $row["number"];
$event["eventsrequired"] = $row["eventsrequired"];
$event["unit"] = $row["unit"];
$event["role"] = $row["role"];
//now we have the data lets try and do something to it
if ($event["role"] == "0") {
//then the user is active so change value to a YES
$event["role"] = "ALL";
}
//add our stock to the portfolio array
$portfolio[] = $event;
//now add individual stats to the array
foreach ($rowsCompleted as $row) {
foreach ($portfolio as &$item) {
if ($item["event"] == $row["name"]) {
//then add our number and date to the array
$item["datecompleted"] = $row["datecompleted"];
$item["number"] = $row["number"];
}
}
}
}
The page still isn't being displayed by chrome so guessing i've missed something.
EDIT4: Page displayed, I am now getting undefined index errors when the table is rendered on the html page.
Specifically the undefined index errors are for date-completed and number. I am taking it to mean that these values are not being added to the portfolio array? Do try and help I have added an Else statement (as below) to ensure that even if the event isn't available the index is:
//now add individual stats to the array
foreach($rowsCompleted as $row)
{
foreach ($portfolio as &$item)
{
if ($item["event"] == $row["name"])
{
//then add our number and date to the array
$item["datecompleted"] = $row["datecompleted"];
$item["number"] = $row["number"];
}
else
{
$item["datecompleted"] = "Never";
$item["number"] = "0";
}
}
}
EDIT 5: Near Success. Sorry to reopen this but I have just noticed behaviour I wasn't expecting: The nested loop only sets the date-completed and number for the first value to matches from the portfolio table. The follow on values are not set. It would seem like the nested loop isn't stepping through all of the portfolio values, and just exiting once the first "event" == "name".
EDIT 6: Sample data and clarification on desired functions:
portfolio sample data
|id|name |number|datacompleted
|21|event1 |3 |2014-07-07
|15|event1 |5 |2014-07-05
|21|event2 |5 |2014-05-08
|15|event1 |1 |2013-05-05
id is the id of the user that completed the event
number is the number of events completed
PrimaryEvents sample data
|id|event |validity|eventsrequired
|1 |event1 |7 |10
|1 |event2 |25 |1
|1 |event3 |12 |50
id is the id of the user that created the entry (used for historic purpose only)
The desired functionality is:
The query should create a an array to allow a html table to be created of everything within the Primary-events table. This table lists the events the user must complete.
The second query or current nested loop should gather the data from the portfolio table for the current user id, then match the event name to the name in the Primary-events array and update (if present) the number and date-completed value. (I.E populate the data for the events that the user has completed).
The current code merges the data from portfolio only for the first match, but then the nested loop seems to exit.
Hopefully this is a more clear description of what I am trying to achieve.
EDIT: I have changed the functionality to use the Left join statement below but am still having problems:
The table only contains some of the events from primary-events table and not all of them. The events being pulled over are only those that the user has completed, the ones the user has not yet completed are not being shown.
EDIT: This query seems to work:
$allEvents = query("SELECT * FROM PrimaryEvents LEFT JOIN portfolio ON (PrimaryEvents.event = portfolio.name) WHERE PrimaryEvents.event = ? AND (portfolio.id = ? Or portfolio.id is null) ORDER BY PrimaryEvents.event", $currentEvent, $_SESSION["id"]);
You need to notice that portfolio is not associative array but multidimensional array, so you cannot access it using $portfolio["event"]. You should use $portfolio[0]["event"], $portfolio[1]["event"] and so on.
It's hard to show you exact solution because I don't know how those arrays/database queries should be merged.
EDIT
It seems your query should look like this:
query("SELECT * FROM PrimaryEvents e LEFT JOIN portfolio p ON e.event = p.name WHERE e.unit = ? AND p.id = ? ORDER BY e.event", $unit,$_SESSION["id"]);
EDIT2
I haven't proposed nested loop (as it's now in modified question) because of performance loss.
You're getting closer with your second query, but still confused about what's in each array.
foreach($rowsCompleted as $row)
{
foreach ($portfolio as &$item) // Need to use reference so we can update it
{
if ($item["event"] == $row["name"])
{
//then add our number and date to the array
$item["datecompleted"] = $row["datecompleted"];
$item["number"] = $row["number"];
break;
}
}
}
To avoid the nested loop, it would be better for $portfolio to be an associative array. Change the code for your initial query to use:
//add our stock to the portfolio array
$portfolio[$event["name"]] = $event;
Then the second loop becomes:
foreach($rowsCompleted as $row)
{
$name = $row["name"];
if (isset($portfolio[$name]) {
$portfolio[$name]["datecompleted"] = $row["datecompleted"];
$portfolio[$name]["number"] = $row["number"];
}
}

Retrieve data from database based on radio button value

I'm quite new to php and mysql so forgive me if I'm doing this completely wrong. I am making a printing balance application and the code below is a part of it.
$command="SELECT itemname FROM items";
$results = mysql_query($command);
while($row = mysql_fetch_assoc($results))
{
foreach ($row as $key => $value) {
print "<input type='radio' name='itemtype' value='$value'>".$value."</input><br />";
}
}
This here is supposedly the price printing form where the user chooses between SHORT BOND PAPER and LONG BOND PAPER (the column itemname from items). The options appear as radio buttons. It works but now I'm stuck with the issue of being able to fetch the price as inserted in their respective rows. Since the itemname and their price are all user-inputted into the database, I'm not supposed to declare their specific price into the code itself, and should be able to retrieve it from the database. I want to be able to get the prices based on the item chosen by the user once they click submit, because I'd need to do this to compute for the price of printing when multiplied with the number of pages later.
I think it's definitely possible but I'm not quite sure how. Theoretically, it would be along the lines of SELECT itemprice FROM items WHERE itemname = $value but ha, I don't think it works that way.
solution edit: here's the complete solution for reference. $compute is just a sample to test if it works while 5 is a sample number of pages that would be entered.
if ($command = mysql_query("SELECT `itemprice` FROM `items` WHERE `itemname` LIKE '" . mysql_escape_string($_POST['itemtype']) . "'"))
{
while($row = mysql_fetch_assoc($command))
{
$compute = $row['itemprice'] * 5;
echo $compute;
}
}
else
{
echo (mysql_error ());
}
It would be something like that indeed.
SELECT itemprice FROM items WHERE itemname = $_POST['itemtype']
assuming that itemprice is the actuial colum of the price. HOwever, doing it like this, makes your code vulnerable to mysql injections. So before you contine, consider reading up a little.

Aggregating an array and displaying the aggregation result alongside the detail

I have a $department variable, which is an instance of a Department class.
The Department class has a property called $employees that contains an array of Employee instances related to that department (retrieved from the database).
The Employee class has a properties called $salary_amount and $job_type_id. The $employees array elements are ordered together by $job_type_id.
For a specified department, I'm outputting a list of employees in that department like so:
Employees for IT Department
----------------------------
<?php foreach($department->employees as $employee): ?>
<?php echo $employee->name; ?>
<?php echo $employee->salary_amount; ?>
<?php endforeach; ?>
What's an efficient way to manipulate $department->employees so I can display a sum-total of the $salary_amount for each $job_type_id alongside the list of employees?
Employees for IT Department
----------------------------
## Help Desk ##
Joseph Lazar
$47,000
John Q. Smith
$15,444
--> Total Salary for Help Desk: $62,444
## Database Administrator ##
Piet Pietersen
$55,120
Ivan Horvat
$63,750
--> Total Salary for Help Desk: $118,870
Current Approach
Currently, $employees is being populated by a single SQL query, ordering the results how I want. I'm then running a separate SQL query to get an array of $salary sum-totals for each $job_type_id using a technique from another question.
However, the resultset from this 2nd query is stored in a different variable $subtotals. Therefore, I still have the problem of efficiently getting the Sum-Total to display "in-context" with the list of employees.
I happen to be using Yii framework, however that's probably irrelevant unless I should think about or approach this type of problem from a totally different perspective? Please tell me in the comments.
Update 1
Here's the current code I'm using, however it feels messy and like there could be a better way. As mentioned above, the aggregation is done by a separate SQL, however I'm open to alternatives.
<?php
$employee_key = 0;
$employees_count = sizeof($department->employees);
$type_sum_key = 0;
$current_type_in_focus = null;
$next_type_in_focus = null;
foreach($department->employees as $employee)
{
$current_type_in_focus = $employee->job_type_id;
$next_type_in_focus = (isset($department->employees[$employee_key+1])) ? $department->employees[$employee_key+1]->job_type_id : null;
# output individual employee data
echo $employee->name;
echo $employee->salary_amount;
# output aggregated total for job type
if ( $current_type_in_focus !== $next_type_in_focus || $employee_key+1 == $employees_count )
{
echo '--> Total Salary for ' . $employee->jobType->name . ': ' . $department->subtotals[$type_sum_key]->total;
$type_sum_key++;
}
$income_key++;
}
?>
One approach would be to use while loops. For example (for clarity the following code sample does not format the output, also it outputs job_type_id rather than a description as that data is not shown in the question):
$employees = $department->employees;
$employee = current($employees);
while($employee)
{
$job_type_id = $employee->job_type_id;
$total_salary = 0;
echo "## Job Type $job_type_id ##";
while ($employee && $employee->job_type_id == $job_type_id)
{
echo $employee->name;
echo $employee->salary_amount;
$total_salary += $employee->salary_amount;
$employee = next ($employees);
}
echo "-->Total Salary for Job Type $job_type_id: $total_salary";
}

Categories