php array , how to display array data in email - php

$qua1 = "5";
$queryNotification= "SELECT * from stock where stockQty <= :qua1 ";
$stmt3 = $conn->prepare($queryNotification);
$stmt3->bindParam(':qua1',$qua1);
$stmt3->execute();
while ($queryNotRow = $stmt3->fetch()){
$a=array("Qty"=>$queryNotRow['stockQty'],"name "=>$queryNotRow['stockName'],"cb "=>$queryNotRow['stockPrice']);
// $array[$queryNotRow['stockQty']] = $queryNotRow['stockName'];
foreach( $a as $row ):
$b = "stockqty = " . $queryNotRow['stockQty'] . " and StockName = " . $queryNotRow['stockName'] . "<br>";
endforeach;
echo $b;
}
If I add send email code inside the foreach loop , the system will send many email according on how many data. How can I only send all the data by ONE EMAIL??

$a=array("Qty"=>$queryNotRow['stockQty'],"name "=>$queryNotRow['stockName']);
foreach($a as $key=>$data){
echo 'stock'.$key.'='.$data.'<br/>';
}

foreach( $a as $row ):
echo "stockqty = " . $row['Qty'] . " and StockName = " . $row['name'] . "<br>";
endforeach;

try this,
$a[] = array(
"Qty" => 1,
"name " => 2
);
foreach ($a as $row) {
echo $row['Qty']."===".$row['name'];
}

Related

How to count/sum this values of the same word in PHP?

I am confused to count these words,
I've some data like this :
web = 1
sistem=1
web=1
sistem=1
web=1
sistem=1
sistem=0
sistem=0
web=0
sistem=0
web=0
sistem=0
web=0
web=0
I want to make result like this :
web = 3
sistem = 3
I'm using array_count_values(), but this result is not good
Array ( [web=1] => 3 [sistem=1] => 3 [sistem=0] => 4 [web=0] => 4 )
My code like this :
foreach ($g as $key => $kata) {
if (strpos($cleanAbstrak, $kata)) {
echo $kata . $ada . "<br>";
$p[] = $kata . "=" . $ada;
// print_r($p);
echo "<br><br>";
} else {
echo $kata, $tidak . "<br>";
$q[] = $kata . "=" . $tidak;
// $m = explode(" ", $q);
// print_r($q);
// echo $q . '<br>';
echo "<br><br>";
}
}
$s = array_merge($p, $q);
echo "<br><br>";
print_r($s);
echo "<br>";
$f = array_count_values($s);
// print_r($f);
echo "<br><br>";
thank you very much if you are willing to help me
RESULT THIS CODE
Another simple way is use a counter like that:
$web=0;
$sistem=0;
foreach ($g as $key => $kata) {
if (strpos($cleanAbstrak, $kata)) {
$sistem=$sistem + $ada;
} else {
$web=$web+$tidak
}
}
echo 'web='.$web.'<br> sistem='.$sistem;
First, you need to separate word and value.
Second, you need to check the value : if it's zero you let it go (can't hold it back anymore). Else you count the value ; if it's written, i suppose it can be greater than 1 ; if it's not, it should be "word", or nothing (which would greatly facilitate counting).
Something like
<?php
$tab = [
'web=1',
'sistem=1',
'web=1',
'sistem=1',
'web=1',
'sistem=1',
'sistem=0',
'sistem=0',
'web=0',
'sistem=0',
'web=0',
'sistem=0',
'web=0',
'web=0',
];
$tab_clean = [];
foreach($tab as $item) {
preg_match('/([a-z]+)=([\d]+)/', $item, $matches);
//print_r($matches);
$word = $matches[1];
$number = $matches[2];
for($i = 0; $i < $number; $i++) {
$tab_clean[] = $word;
}
}
$tab_count = array_count_values($tab_clean);
print_r($tab_count);
?>

Why is my for loop displaying the same item twice?

I am trying to display each item in a JSON array of reviews. There are two reviews in the array, but instead of displaying both of them, this code displays same one twice.
Why is this happening?
$response = curl_exec($curl);
$data = json_decode($response, true);
// Setup the Review for posting to the Mysql Database
// Loop through Reviews
for ($i=0; $i<count($data['reviews']); $i++) {
$id = $data['reviews']['0']['id']; // good
$stars = $data['reviews']['0']['stars']; //good
$title = $data['reviews']['0']['title'];
$text = $data['reviews']['0']['text'];
$createdAt = $data['reviews']['0']['createdAt'];
$companyReply = $data['reviews']['0']['companyReply']['text'];
$companyReplyDate = $data['reviews']['0']['companyReply']['createdAt'];
// Do Mysql insert for each row
// show this
echo "id: " . $id . "</br>";
echo "stars: " . $stars . "</br>";
echo "title: " . $title . "</br>";
echo "text: " . $text . "</br>";
echo "Created: " . $createdAt . "</br>";
echo "Reply: " . $companyReply . "</br>";
echo "Reply Date: " . $companyReplyDate . "</br> </br>";
}
curl_close($curl);
It is because you aren't actually using your loop increment variable to get different array keys. You're just selecting the ['0'] index repeatedly. Instead, use $i as the array key like this:
for ($i=0; $i<count($data['reviews']); $i++) {
$id = $data['reviews'][$i]['id']; // good
$stars = $data['reviews'][$i]['stars']; //good
$title = $data['reviews'][$i]['title'];
$text = $data['reviews'][$i]['text'];
$createdAt = $data['reviews'][$i]['createdAt'];
$companyReply = $data['reviews'][$i]['companyReply']['text'];
$companyReplyDate = $data['reviews'][$i]['companyReply']['createdAt'];
// ... etc.

Foreach loop "Group By" Key in PHP Array

I am so close to figuring this out, this is using Wordpress and Gravity Forms Plugin:
<?php
$form_id = '1'; // The registration form's ID
$entries = GFAPI::get_entries( $form_id ); // Get all entries
$html = "<table class='ent'>"; // Create an HTML table
$html .= "<tr><th>Name</th><th>Organization</th><th>Event</th><th>Date</th><th>Number Attending</th></tr>";
$list = array(); // Array to store the original fields
$used = array(); // Array to stored the "used" fields
// Loop through array of entries,each element
foreach($entries as $entry){
// Get datepart from datetime of event and today's date
$date = date_format(new DateTime($entry['8']), 'Y-m-d');
$today = date("Y-m-d");
// Compare event's date with current date
if ( $date >= $today ) {
$list[] = $entry;
if (!empty($used[$entry['6']])) // If used[event name] not null then sum associated numattend
{
$used[$entry['6']] += $entry['5'];
}
else // return associated numattend
{
$used[$entry['6']] = $entry['5'];
}
}
}
unset($entry);
foreach ($used as $key => $value) {
foreach ($list as $key1 => $value1) {
$html .= "<tr><td>" . $value1['1'] . " " . $value1['2'] . "</td><td>" . $value1['93'] . "</td><td>" . $value1['6'] . "</td><td>" . $value1['8'] . "</td><td>" . $value1['5'] . "</td></tr>";
}
$html .= "<tr class='ent_sum_rw'><td>" . " " . "</td><td>" . " " . "</td><td>" . " " . "</td><td class='ent_sum'>" . "Total Attending:" . "</td><td>" . $value . "</td></tr>";
}
unset($value);
$html .= "</table>";
return $html;
?>
Result:
I want the loop to end the entry list on the corresponding "Event Name", Like "Test Event 22" should not show up on the top entry list.
The trick is to populate an Array that meets your demands and after that convert it to html. I haven't tested the code, but it should be clear how it works.
$aEventList = Array();
foreach($entries as $entry){
if (!isset($aEventList[$entry['7']])){ //7 is id of event
//if this event is not made, create it and add name and the date, enter the right numeral
$aEventList[$entry['7']]['name'] = $entry['6'];
$aEventList[$entry['7']]['date'] = $entry['90'];
$aEventList[$entry['7']]['users'] = Array();
}
$aEventList[$entry['7']]['users'][] = Array('name' => $entry['1'] . ' '. $entry['2'],
'org' => $entry['93'],
'num' => $entry['5']);
}
//now you've populated every event and you can loop through it on your desired way:
foreach ($aEventList as $iEvent => $aEvent){
echo $aEvent['name']; //event name
echo $aEvent['date']; // event date
foreach ($aEvent['users'] as $aEventUser){
echo $aEventUser['name']; // user name
echo $aEventUser['org']; // user organisation
echo $aEventUser['num']; // num attendees
}
}

How to use mysql to find a field name of a record?

PDO sql codes :
while($r = $q->fetch(PDO::FETCH_ASSOC)){
$gg = join('</td><td>', $r);
echo "<tr><td>" . $no_count . "</td><td>" . $gg . "</td></tr>";
$no_count = $no_count + 1;
}
variable $r is the record, how can I echo the field name of $r?
Let's say $r carry records from 2 different fields "product" and "price". The value of $r is "apple", "130". How can I add "usd" into "130"?
I need something like.... if $field_name == "$r['price']" { $r = join('usd', $r); };
Thanks to Mike B, I almost there :
while($r = $q->fetch(PDO::FETCH_ASSOC)){
foreach ($r as $name => $value) {
if ($name == "price"){
$r = "usd" . $value; // this line got problem, how to change the value in an array variable $r?
}
}
$gg = join('</td><td>', $r);
echo "<tr><td>" . $no_count . "</td><td>" . $gg . "</td></tr>";
$no_count = $no_count + 1;
}
array_keys($r) will get you a list of fields from the table since you're fetching an associative array.
You can also loop through $r:
foreach ($r as $name => $value) {
print "$name: " . $value;
}
Update
// this line got problem, how to change the value in an array variable $r?
$r[$name] = 'usd' . $value;
Make the edit to the original name. Since you have the key in the $name variable from the foreach loop you can set it directly.

php counter is being reset each loop

I've got a rather complicated set of loops that pulls data out of mysql and compares it to values in an array and increments a counter. When I echo a flag when the counter is incremented, I get a bijilion flags (there're like 2600 records returned from the mysql query). But each time it prints, the counters are always 1 and when I print the counter's value at the end, it shows up as zero. It seems like something is re-setting the counter…
code
# ARRAY
$demographics=array(
"region"=>array(
"Northeast"=>array('total'=>0,'consented'=>0,'completed'=>0),
//more...
"West"=>array('total'=>0,'consented'=>0,'completed'=>0)
),"societal envirn"=>array(
"Urban"=>array('total'=>0,'consented'=>0,'completed'=>0)
),"age"=>array(
'18-19'=>array('total'=>0,'consented'=>0,'completed'=>0),
'20-24'=>array('total'=>0,'consented'=>0,'completed'=>0),
//more...
'55-59'=>array('total'=>0,'consented'=>0,'completed'=>0)
),
//more...
);
# LOOPS
while ($dbrecord = mysql_fetch_assoc($surveydata)) {
foreach ( $dbrecord as $dbfield=>$dbcellval ) {
foreach ( $demographics as $demographic=>$options ) {
foreach ( $options as $option=>&$counter ) {
if($demographic==="age"){
list($min,$max) = explode('-', $option);
if ($dbcellval >= $min && $dbcellval <= $max){
$counter['total']++;
echo '$' . $option . "['total'] = " . $counter['total'] . "<br />";
if ($dbrecord['consent']==="1"){
$counter['consented']++;
echo '$' . $option . "['consented'] = " . $counter['consented'] . "<br />";
if ($dbrecord['completion status']==="complete") {
$counter['completed']++;
echo '$' . $option . "['completed'] = " . $counter['completed'] . "<br />";
break 3;
} // if
} // if
break 2;
}
} // if age
else if ($option===$dbcellval){
$counter['total']++;
echo '$' . $option . "['total'] = " . $counter['total'] . "<br />";
if ($dbrecord['consent']==="1"){
$counter['consented']++;
echo '$' . $option . "['consented'] = " . $counter['consented'] . "<br />";
if ($dbrecord['completion status']==="complete") {
$counter['completed']++;
echo '$' . $option . "['completed'] = " . $counter['completed'] . "<br />";
break 3;
} // if
} // if
break 2;
} // else if $option==$dbcellval
} // foreach $options
} // foreach $demographics
} // foreach $dbrecord
} // while
sample output
$40-44['total'] = 1
$White['total'] = 1
$35-39['total'] = 1
$Northeast['total'] = 1 // the 'total' counter is 1
$Northeast['consented'] = 1
$Northeast['completed'] = 1
$South['total'] = 1
$Northeast['total'] = 1 // notice the 'total' counter is 1 again :(
$Northeast['consented'] = 1
$Northeast['completed'] = 1
You're defining counter from a foreach instruction, as $value in foreach($foo as $key=>$value), when using the foreach you only have a local copy of $counter.
You need to use either foreach($foo as $key=>&$value) or to refer to the full array path of your counter from $demographics.
You need to reference your array at each level, otherwise you are working on a copy of the data:
foreach ( $dbrecord as $dbfield=>$dbcellval ) {
foreach ( $demographics as $demographic => &$options ) {
foreach ( $options as $option => &$counter ) {
if($demographic==="age"){
list($min,$max) = explode('-', $option);
if ($dbcellval >= $min && $dbcellval <= $max){
$counter['total']++;
What if you simply stick with the $demographics variable.
i.e.
...
foreach ( $options as $option ) {
...
$demographics[$option]['total']++;
...

Categories