$query_ListAbsence = "SELECT count(dated) as dtd, classed, name FROM att group by dtd, classed by dtd desc";
$la = mysql_query($query_la, $sshost) or die(mysql_error());
$row_la = mysql_fetch_assoc($la);
do{
....
}while ($row_la = mysql_fetch_assoc($la));
I want to result to show as
--date--
name1 - classed1
name2 - classed2
--another date---
namea - classeda
nameb - classedb
and so on...
But when inside the do{}while loop the date is displayed the number of times the loop runs. Can i display the results as shown with a single table. This is done by MySQL / PHP.
$arr = array();
while($row = mysql_fetch_assoc($la)) {
$arr[$row['your_date']][] = $row;
}
And than loop through $arr array;
Related
The issue is not how I am generating the number, that is working fine for a different app I have. The issue is counting the total number of boxes and iterating through a forloop that many times. Right now I am multiplying box_ct * qty_ord to get the total boxes. BTW, I am still trying to understand everything I can do with foreach and for loops, so if anyone has a better idea, Great.
CLARIFICATION: I am searching the database by order number and pulling back qty_ord, box_ct, and item_no for each. On some orders there is more than one item. So for each item I need to multiply qty_ord by box_ct = count and for that specific item I need to iterate through the loop based on what my count is.Right now if I remove the foreach and hardcode the for loop count variable it works fine. Any ideas on what is wrong with my foreach?
qty_ord | box_ct | item_no
------------------------
1 2 10001
3 3 10002
2 1 10003
2 3 10004
2 6 10005
Below is my code.
$sql ="SELECT imitmidx_sql.box_ct, oeordlin_sql.item_no, oeordlin_sql.qty_ord
FROM imitmidx_sql
JOIN oeordlin_sql ON oeordlin_sql.item_no = imitmidx_sql.item_no WHERE ord_no ='$orderNum'";
$query = odbc_exec($conn, $sql);
$row7 = odbc_fetch_array($query);
foreach($row7['item_no'] as $item){
$count = $row7['qty_ord'] * $row7['box_ct'];
for($counter = 0; $counter <= $count; $counter++){
//GRAB NUMBER FROM FILE
$sscc = file_get_contents("ucc128.txt");
//INCREMENT NUMBER
$sscc++;
//PUT THE NUMBER BACK IN FILE
$sscc = file_put_contents("ucc128.txt", $sscc);
//COMBINE STATIC NUMBER AND NUMBER FROM FILE
$ssccnumber = "00950000".$sscc;
//CREATE CHECK DIGIT MATH
$ssccnumArray = str_split((string)$ssccnumber);
$ssccstep1 = array_combine(range(1, count($ssccnumArray)), $ssccnumArray);
$ssccstep2 = 0;
$ssccstep4 = 0;
foreach($ssccnumArray as $k => $v){
if($k%2){
$ssccstep2 += (int)$v;
}else{
$ssccstep4 += (int)$v;
}
}
$ssccstep3 = (int)$ssccstep2 * 3;
$ssccstep5 = (int)$ssccstep3 + (int)$step4;
$ssccCheckDigit = (ceil($ssccstep5 / 10) * 10) - $ssccstep5;
//END CREATE CHECK DIGIT
//CONCATENATE FULL NUMBER
$sscc1 = $ssccnumber.$ssccCheckDigit;
$sql = "INSERT INTO ucc128 (sscc, ord_no) VALUES ('$sscc1' ,'$orderNum')";
#echo $sql.'<br />';
odbc_exec($connPRONUMBER, $sql);
}
}
I believe your issue may be with the following:
foreach($row7['item_no'] as $item){
$count = $row7['qty_ord'] * $row7['box_ct'];
//...
}
This should be:
foreach($row7['item_no'] as $item){
$count = $item['qty_ord'] * $item['box_ct'];
//...
}
You are grabbing data from the query results, as opposed to the current row.
My foreach was not working, so what I did instead was get the sum using my SQL query.
SELECT SUM(imitmidx_sql.box_ct * oeordlin_sql.qty_ord) AS total
FROM imitmidx_sql
JOIN oeordlin_sql ON oeordlin_sql.item_no = imitmidx_sql.item_no WHERE ord_no ='$orderNum'
I was then able to ouput it and save $row7['total'] to the variable $count.
I'm pulling multiple rows from a table and formatting them to this standard: 3 Swords, 5 Daggers, etc.
Well When I try to put that data into a json Array, It's only pulling the last result as this [{"weapons":"You Used: 3 Rusty Dagger's, "}] Which it should say: [{"weapons":"You Used: 3 Rusty Dagger's, 2 Swords"}]
Here's The Query I'm currently using, Which will show perfectly inside of the while loop:
$get_weapons = mysql_query("SELECT
O.player_id,
O.item_id,
O.name,
O.attack,
O.defense,
O.type,
O.owned,
(SELECT
sum(owned) FROM items_owned
WHERE owned <= O.owned AND player_id=$id) 'RunningTotal'
FROM items_owned O
HAVING RunningTotal <= $mob_avail
ORDER BY attack DESC");
// Get Weapon Info
while($weapon = mysql_fetch_array($get_weapons)){
$weapon_id = $weapon['item_id'];
$weapon_name = $weapon['name'];
$weapon_attack = $weapon['attack'];
$weapon_defense = $weapon['defense'];
$weapon_owned = $weapon['owned'];
// Formatting Weapons Message
$weapon_message = 'You Used: '.$weapon_owned.' '.$weapon_name.'\'s, ';
}
$data[] = array('weapons'=>$weapon_message);
echo json_encode($data);
I understand that the $data array is outside of the while loop, But I'm only needing a total of one arrays, so I'm kind of stuck on what to do to fix this issue. Any help would be awesome
You should add one static variable before append the results into the message.
Here is the reference answer:
$get_weapons = mysql_query("SELECT O.player_id, O.item_id, O.name, O.attack, O.defense, O.type, O.owned, (
SELECT sum(owned) FROM items_owned WHERE owned <= O.owned AND player_id=$id) 'RunningTotal' FROM items_owned O HAVING RunningTotal <= $mob_avail ORDER BY attack DESC");
// Initialize the message
$weapon_message = 'You Used: ';
// Get Weapon Info
while($weapon = mysql_fetch_array($get_weapons)){
$weapon_id = $weapon['item_id'];
$weapon_name = $weapon['name'];
$weapon_attack = $weapon['attack'];
$weapon_defense = $weapon['defense'];
$weapon_owned = $weapon['owned'];
// Formatting Weapons Message
$weapon_message = $weapon_message.$weapon_owned.' '.$weapon_name.'\'s, ';
}
$data[] = array('weapons'=>$weapon_message);
echo json_encode($data);
With the following code:
//turn items into an array
$item_array = array('abc','xyz2','Good','nice-b');
//implode items, turn into string
$item_implode = join("','", $item_array);
//declare an overall array for result
$product_items = array();
$productList = array();
$result = $mysqli->query("SELECT Name, WebsitePrice as price, WebsiteStock as stock from table_products where Name IN ('$item_implode')");
if ($result->num_rows > 0) {
$x = 1;
// output data of each row
while($row = $result->fetch_assoc()) {
$product_items[$x]["Name"] = $row['Name'];
$product_items[$x]["price"] = $row['price'];
$product_items[$x]["stock"] = $row['stock'];
$x = $x + 1;
}
} else {
echo "0 results";
}
I'm getting this output:
abc- 99 - yes
xyz - 20 - yes
Good - 30 - yes
nice-b - 55 - yes
But when I use an item called Hello1 instead of Good, like this:
$item_array = array('abc','xyz2','Hello1','nice-b');
I'm getting this output:
abc- 99 - yes
Hello1 - 77 - yes
xyz - 20 - yes
nice-b - 55 - yes
Meaning that the name of the object is causing some change in the order of the array, and it becomes the second item, even though it should be the third one.
What's causing this?
Use ORDER BY FIELD(Name, 'abc','xyz2','Good','nice-b'); in your query. You could use $item_implode for reusability.
[Fetched from comments]
In the SQL world, order is not an inherent property of a set of data. Thus, you get no guarantees from your RDBMS that your data will come back in a certain order -- or even in a consistent order -- unless you query your data with an ORDER BY clause.
There is no guarantee that MySQL will return the results in the order you set the IDs in the IN clause.
Later edit: Based on your last comment you can do something like this:
if ($result->num_rows > 0) {
$product_items = array_flip($item_array);
// output data of each row
while($row = $result->fetch_assoc()) {
$product_items[$row['Name']] = array();
$product_items[$row['Name']]["Name"] = $row['Name'];
$product_items[$row['Name']]["price"] = $row['price'];
$product_items[$row['Name']]["stock"] = $row['stock'];
}
}
My query is:
$sql="SELECT COUNT(`Variant`) AS tsold, MONTHNAME(`sold_date`) AS mname FROM `vehicle_sold` GROUP BY MONTH(`sold_date`) ";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)) {
echo $row["mname"],"---", $row["tsold"],"<br />";
}
which gives me the below result:
January---1
February---2
March---7
April---11
May---6
July---1
There are no sales in June so what I want is the query to return "June---0" for example. If it also shows the next months up to December it's ok. I'd like the following output:
January---1
February---2
March---7
April---11
May---6
June---0
July---1
Aug---0
Sept---0
Oct---0
Nov---0
Dec---0
Use an array of month names -
$months = array('January', 'February', ..., 'December');
Generate and array with the data returned from database -
while($row = mysqli_fetch_array($result)) {
$data[$row["mname"]] = $row["tsold"];
}
And print them accordingly -
foreach($months as $value) {
echo $value.' - '. (array_key_exists($value, $data) ? $data[$value] : 0) . '<br/>';
}
I assume you have actually no data in the database if you did not sell anything. Hence its a bit hard to generate a month without info.
You want an array with all the months, corresponding with the amount of sales.
I would suggest you make something like this:
Prepare an array with the months with all the values on 0 as default (you can make this array list 'nicer', but just now as example).
$data['March'] = 0;
$data['April'] = 0;
Now you can iterate like you did
while($row = mysqli_fetch_array($result)) {
$data[$row["mname"]] = $row["tsold"];
}
If it does not get filled by the database, the value would still be 0. Otherwise it gets overwritten by the database value.
Here First of all I fetched all tags from database. (tags are string like marketting,jobs etc).
$POS is array of POS(parts of speech words). For each words in this array, for given tag, I am calculating probability using writtten equation.
As it is appearing, two nested loops are there. To make decision, I need highest value from array $prob and tag belonging to that value.
Here in this code I will get all values in array $prob and from that I can get highest value, but how can I persist tag value too? is there any other data strcture in PHP to manage this short of scenario?
$selectTag = mysqli_query($con,"SELECT tag from koove_tag");
$prob = array();
$i=0;
while ($row1 = #mysqli_fetch_array($selectTag))
{
foreach($POS as $p)
{
//Calculate total pos for given tag
$selectPOS = mysqli_query($con,"SELECT * from koove_post where tag = '".$row1[tag]."'");
while ($row2 = #mysqli_fetch_array($selectTag))
{
$totalPOSs.=$row2[pos].",";
}
$totalPOS_count = str_word_count($totalPOSs);
//calculate how many times particular 'pos' appears for given tag
$selectPOS = mysqli_query($con,"SELECT * from koove_post where tag = '".$row1[tag]."'");
while ($row3 = #mysqli_fetch_array($selectTag))
{
$POSs.=$row3[pos].",";
}
$pos_Count = echo substr_count($string, $p);
//calculate distinct POS in all POSs for all tags
$selectPOS = mysqli_query($con,"SELECT pos from koove_post");
while ($row4 = #mysqli_fetch_array($selectTag))
{
$POSs.=$row4[pos].",";
}
$distinct_pos_Count = echo substr_count($string, $p);
$prob[$i] = ($pos_Count + 1)/ ($totalPOS_count + $distinct_pos_Count);
$i++;
}
}
There are large numbers(thousands) of posts, if is there any better approach to faster the processing that also welcome.
Certainly. Use the stdClass() object to hold the values that you need. Then write method to sort them. For example:
$prbo[$i] = new stdClass();
$prob[$i]->value = ($pos_Count + 1)/ ($totalPOS_count + $distinct_pos_Count);
$prob[$i]->tag = $row1[tag];
After you build that array, then write a custom sort function.
http://www.php.net/manual/en/array.sorting.php
1.You can use join query that will eliminate one of cycles.
2.You can get highest value with end(array) , or custom sort function.