Mysql_fetch_row not populating SESSION array with multiple results - php
Hello Here is my solution as i took some advice about the SQL statement ... thank you all
$connection = ConnectionBD();
$tag = $_SESSION['nomUtilisateur'];
$panier= array();
$requete = mysql_query("SELECT * FROM cours INNER JOIN elevecours ON cours.idcours = elevecours.IDCours WHERE elevecours.IDEleve= '$tag'",$connection);
$H = 0;
if(mysql_num_rows($requete) != 0)
{
while($row = mysql_fetch_row($requete))
{
//$panier[] = $row;
$_SESSION['Panier']['Id'][$H] = $row[0];
$_SESSION['Panier']['CodeCours'][$H] = $row[1];
$_SESSION['Panier']['Titre'][$H] = $row[2];
$_SESSION['Panier']['Prealable'][$H] = $row[3];
$_SESSION['Panier']['NbHeure'][$H] = $row[4];
$_SESSION['Panier']['Session'][$H] = $row[5];
$_SESSION['Panier']['Credit'][$H] = $row[6];
$H ++;
}
}
mysql_close($connection);
You need to mysql_fetch_row once per iteration in your loop.
for($i = 0; $i < mysql_num_rows($req); $i++) {
$resultat = mysql_fetch_row($req);
//the rest of your code goes here
}
Inside your second foreach loop you are setting $H = 0; and then $H++ inside the same loop, this will not increment properly as the next time it goes over the loop again the $H = 0; statement will result this to 0 and therefore keep overwriting your data until the loop is done and you're left with only one $_SESSION value at the end.
for($j = 0; $j < count($panier); $j++)
{
if($resultat[$i] == $panier[$j][0])
{
$H = 0; <-- sets to zero on every interation
$_SESSION['Panier']['Id'][$H] = $result[0];
$_SESSION['Panier']['CodeCours'][$H] = $result[1];
$_SESSION['Panier']['Titre'][$H] = $result[2];
$_SESSION['Panier']['Prealable'][$H] = $result[3];
$_SESSION['Panier']['NbHeure'][$H] = $result[4];
$_SESSION['Panier']['Session'][$H] = $result[5];
$_SESSION['Panier']['Credit'][$H] = $result[6];
$H++;
}
}
Suggest you move this outside your loop if the intention is to iterate the $H variable.
these are my testing variables...
$session = array();
$panier = array(array(1,'one'),array(2,'two'),array(3,'three'),);
$req = array(
array(1,'one','Titre1','Prealable1','NbHeure1','Session1','Credit1'),
array(2,'two','Titre2','Prealable2','NbHeure2','Session2','Credit2'),
array(3,'three','Titre3','Prealable3','NbHeure3','Session3','Credit3'),
array(4,'four','Titre4','Prealable4','NbHeure4','Session4','Credit4'),
array(5,'five','Titre5','Prealable5','NbHeure5','Session5','Credit5'),);
i wrote the code to not rely on your DB and data exactly, since I don't know what it is... dunno. switch out some of these lines of code to use your database stuff.
// $req = mysql_query("SELECT * FROM cours", $connection); uncomment this line for your code
if ($req!==false)
{
foreach( $req as $i => $resultat ) // testing row, remove for real
// while( $resultat = mysql_fetch_row($req) ) // real row, uncomment for you
{
$compare = $resultat[0];
// we will be using $compare to see if this row
// matches one in $panier.. this needs to be changed
// appropriately. I don't have enough info to define it....
// this needs to be a unique field, or it should be
// it is going to be used like this: if($resultat[$i] == $panier[$j][0])
$result[ $compare ] = $resultat;
}
$hi = 0;
foreach( $panier as $index => $panier_row )
{
$compare = $panier_row[0];
if (isset($result[$compare]))
{
$H = 0;
// change $session to $_SESSION for your code...
$session['Panier']['Id'][$hi][$H] = $result[$compare][0];
$session['Panier']['CodeCours'][$hi][$H] = $result[$compare][1];
$session['Panier']['Titre'][$hi][$H] = $result[$compare][2];
$session['Panier']['Prealable'][$hi][$H] = $result[$compare][3];
$session['Panier']['NbHeure'][$hi][$H] = $result[$compare][4];
$session['Panier']['Session'][$hi][$H] = $result[$compare][5];
$session['Panier']['Credit'][$hi][$H] = $result[$compare][6];
$H++;
$hi++;
}
}
}
my results show $session['Panier']['Credit'] as having three things for 1, 2, 3 but not 4 or 5 because they don't match $panier.
what is in $session at the end of all that:
array (
'Panier' =>
array (
'Id' =>
array (
0 =>
array (
0 => 1,
),
1 =>
array (
0 => 2,
),
2 =>
array (
0 => 3,
),
),
'CodeCours' =>
array (
0 =>
array (
0 => 'one',
),
1 =>
array (
0 => 'two',
),
2 =>
array (
0 => 'three',
),
),
'Titre' =>
array (
0 =>
array (
0 => 'Titre1',
),
1 =>
array (
0 => 'Titre2',
),
2 =>
array (
0 => 'Titre3',
),
),
'Prealable' =>
array (
0 =>
array (
0 => 'Prealable1',
),
1 =>
array (
0 => 'Prealable2',
),
2 =>
array (
0 => 'Prealable3',
),
),
'NbHeure' =>
array (
0 =>
array (
0 => 'NbHeure1',
),
1 =>
array (
0 => 'NbHeure2',
),
2 =>
array (
0 => 'NbHeure3',
),
),
'Session' =>
array (
0 =>
array (
0 => 'Session1',
),
1 =>
array (
0 => 'Session2',
),
2 =>
array (
0 => 'Session3',
),
),
'Credit' =>
array (
0 =>
array (
0 => 'Credit1',
),
1 =>
array (
0 => 'Credit2',
),
2 =>
array (
0 => 'Credit3',
),
),
),
)
personally, though... you should use mysql_fetch_assoc and do it like this:
$session = array();
$panier = array(array(1,'one'),array(2,'two'),array(3,'three'),);
$req = array(
array('id'=>1,'var'=>'one','Titre'=>'Titre1','Prealable'=>'Prealable1','NbHeure'=>'NbHeure1','Session'=>'Session1','Credit'=>'Credit1'),
array('id'=>2,'var'=>'two','Titre'=>'Titre2','Prealable'=>'Prealable2','NbHeure'=>'NbHeure2','Session'=>'Session2','Credit'=>'Credit2'),
array('id'=>3,'var'=>'thr','Titre'=>'Titre3','Prealable'=>'Prealable3','NbHeure'=>'NbHeure3','Session'=>'Session3','Credit'=>'Credit3'),
array('id'=>4,'var'=>'fou','Titre'=>'Titre4','Prealable'=>'Prealable4','NbHeure'=>'NbHeure4','Session'=>'Session4','Credit'=>'Credit4'),
array('id'=>5,'var'=>'fiv','Titre'=>'Titre5','Prealable'=>'Prealable5','NbHeure'=>'NbHeure5','Session'=>'Session5','Credit'=>'Credit5'),);
// $req = mysql_query("SELECT * FROM cours", $connection);
if ($req!==false)
{
foreach( $req as $i => $resultat ) // testing row, remove for real
// while( $resultat = mysql_fetch_assoc($req) ) // real row, uncomment for you
// mysql_fetch_assoc !!
{
$compare = $resultat['id'];
// we will be using $compare to see if this row
// matches one in $panier.. this needs to be changed
// appropriately. I don't have enough info to define it....
// this needs to be a unique field, or it should be
$result[ $compare ] = $resultat;
}
foreach( $panier as $index => $panier_row )
{
$compare = $panier_row[0];
if (isset($result[$compare]))
{
$session[] = $result[$compare];
}
}
}
and this is $session
array (
0 =>
array (
'id' => 1,
'var' => 'one',
'Titre' => 'Titre1',
'Prealable' => 'Prealable1',
'NbHeure' => 'NbHeure1',
'Session' => 'Session1',
'Credit' => 'Credit1',
),
1 =>
array (
'id' => 2,
'var' => 'two',
'Titre' => 'Titre2',
'Prealable' => 'Prealable2',
'NbHeure' => 'NbHeure2',
'Session' => 'Session2',
'Credit' => 'Credit2',
),
2 =>
array (
'id' => 3,
'var' => 'thr',
'Titre' => 'Titre3',
'Prealable' => 'Prealable3',
'NbHeure' => 'NbHeure3',
'Session' => 'Session3',
'Credit' => 'Credit3',
),
)
Related
PHP Pushing variables to array1 then push it to array2 in a loop
I am having trouble to get the target result of an array. What I am trying to do is: push every variables in $inner_data in every loop. then push that $inner_data to $details_data to create an array inside an array I hope I explain my idea and problem. Code $details_data = array(); $inner_data = array(); for($i=0; $i < $count_selected; $i++){ $amount[$i] = $quantity[$i] * $price[$i]; array_push($inner_data, $last_insert_id, $amount[$i], $quantity[$i], $products[$i]); array_push($details_data, $inner_data); } print_r($details_data); exit; Target result $data = array( array( 'last_insert_id' => 10, 'amount' => 20000, 'quantity' => 1, 'product_id' => 1 ), array( 'last_insert_id' => 10, 'amount' => 1000, 'quantity' => 1, 'product_id' => 2 ) ); Unexpected Result Array ( [0] => Array ( [0] => 10 [1] => 20000 [2] => 1 [3] => 1 ) [1] => Array ( [0] => 10 [1] => 20000 [2] => 1 [3] => 1 [4] => 10 [5] => 10000 [6] => 1 [7] => 2 ) )
for($i=0; $i < $count_selected; $i++){ $amount[$i] = $quantity[$i] * $price[$i]; $inner_data=array( 'last_insert_id'=>$last_insert_id, 'amount'=>$amount[$i], 'quantity'=>$quantity[$i], 'product_id'=>$products[$i] ); array_push($details_data, $inner_data); }
Link for your reference <?php $details_data = array(); /* do not set it as a global vari OR clear it when you re-use it */ //$inner_data = array(); $last_insert_id = 10; $amount = [20000, 1000]; $quantity = [110, 220]; $price = [32, 64]; $products = [001, 002]; for($i=0; $i < 2; $i++){ $amount[$i] = $quantity[$i] * $price[$i]; /* set it here as a local vari, thus it won't accumulate the result */ $inner_data = array(); //array_push($inner_data, $last_insert_id, $amount[$i], $quantity[$i], $products[$i]); $inner_data['last_insert_id'] = $last_insert_id; $inner_data['amount'] = $amount[$i]; $inner_data['quantity'] = $quantity[$i]; $inner_data['product_id'] = $products[$i]; array_push($details_data, $inner_data); } echo "<pre>"; print_r($details_data); exit; echo "</pre>"; ?>
array_push($details_data, array('last_insert_id'=>$last_insert_id, 'amount'=>$amount[$i], 'quantity'=>$quantity[$i], 'product_id'=>$products[$i]) ); Try to push an array directly to details_data.
Add item to array of arrays based on id
I have an array of arrays set up like so. There are a total of 10 arrays but I will just display the first 2. The second column has a unique id of between 1-10 (each only used once). Array ( [0] => Array ( [0] => User1 [1] => 5 ) [1] => Array ( [0] => User2 [1] => 3 ) ) I have another array of arrays: Array ( [0] => Array ( [0] => 3 [1] => 10.00 ) [1] => Array ( [0] => 5 [1] => 47.00 ) ) where the first column is the id and the second column is the value I want to add to the first array. Each id (1-10) is only used once. How would I go about adding the second column from Array#2 to Array#1 matching the ID#?
There are tons of ways to do this :) This is one of them, optimizing the second array for search and walking the first one: Live example <? $first_array[0][] = 'User1'; $first_array[0][] = 5; $first_array[1][] = 'User2'; $first_array[1][] = 3; $secnd_array[0][] = 3; $secnd_array[0][] = 10.00; $secnd_array[1][] = 5; $secnd_array[1][] = 47.00; // Make the user_id the key of the array foreach ($secnd_array as $sca) { $searchable_second_array[ $sca[0] ] = $sca[1]; } // Modify the original array array_walk($first_array, function(&$a) use ($searchable_second_array) { // Here we find the second element of the first array in the modified second array :p $a[] = $searchable_second_array[ $a[1] ]; }); // print_r($first_array);
Assuming that 0 will always be the key of the array and 1 will always be the value you'd like to add, a simple foreach loop is all you need. Where $initial is the first array you provided and $add is the second: <?php $initial = array(array("User1", 5), array("User2", 3)); $add = array( array(0, 10.00), array(1, 47.00)); foreach ($add as $item) { if (isset($initial[$item[0]])) { $initial[$item[0]][] = $item[1]; } } printf("<pre>%s</pre>", print_r($arr1[$item[0]], true));
I don't know if I got you right, but I've come up with a solution XD <?php $array_1 = array( 0 => array( 0 => 'ID1', 1 => 5 ), 1 => array( 0 => 'ID2', 1 => 3 ) ); $array_2 = array( 0 => array( 0 => 3, 1 => 10.00 ), 1 => array( 0 => 5, 1 => 47.00 ) ); foreach($array_1 as $key_1 => $arr_1){ foreach($array_2 as $key_2 => $arr_2){ if($arr_2[0] == $arr_1[1]){ $array_1[$key_1][2] = $arr_2[1]; } } } var_dump($array_1); ?> Demo: https://eval.in/201648 The short version would look like this: <?php $array_1 = array(array('ID1',5),array('ID2',3)); $array_2 = array(array(3,10.00),array(5,47.00)); foreach($array_1 as $key => $arr_1){ foreach($array_2 as$arr_2){ if($arr_2[0] == $arr_1[1]){ $array_1[$key][2] = $arr_2[1]; } } } var_dump($array_1); ?> Demo: https://eval.in/201649 Hope that helps :)
A quick and dirty way just to show you one of the more self-explaining ways to do it :) $users = array( 0 => array( 0 => 'User1', 1 => 123 ), 1 => array( 0 => 'User2', 1 => 456 ) ); $items = array( 0 => array( 0 => 123, 1 => 'Stuff 1' ), 1 => array( 0 => 456, 1 => 'Stuff 2' ) ); foreach($items as $item){ foreach($users as $key => $user){ if($item[0] == $user[1]) array_push($users[$key], $item[1]); } }
PHP Counting inside an Array
I want to create a list where if its already in the array to add to the value +1. Current Output [1] => Array ( [source] => 397 [value] => 1 ) [2] => Array ( [source] => 397 [value] => 1 ) [3] => Array ( [source] => 1314 [value] => 1 ) What I want to Achieve [1] => Array ( [source] => 397 [value] => 2 ) [2] => Array ( [source] => 1314 [value] => 1 ) My current dulled down PHP foreach ($submissions as $timefix) { //Start countng $data = array( 'source' => $timefix['parent']['id'], 'value' => '1' ); $dataJson[] = $data; } print_r($dataJson);
Simply use an associated array: $dataJson = array(); foreach ($submissions as $timefix) { $id = $timefix['parent']['id']; if (!isset($dataJson[$id])) { $dataJson[$id] = array('source' => $id, 'value' => 1); } else { $dataJson[$id]['value']++; } } $dataJson = array_values($dataJson); // reset the keys - you don't nessesarily need this
This is not exactly your desired output, as the array keys are not preserved, but if it suits you, you could use the item ID as the array key. This would simplify your code to the point of not needing to loop through the already available results: foreach ($submissions as $timefix) { $id = $timefix['parent']['id']; if (array_key_exists($id, $dataJson)) { $dataJson[$id]["value"]++; } else { $dataJson[$id] = [ "source" => $id, "value" => 1 ]; } } print_r($dataJson);
You should simplify this for yourself. Something like: <? $res = Array(); foreach ($original as $item) { if (!isset($res[$item['source']])) $res[$item['source']] = $item['value']; else $res[$item['source']] += $item['value']; } ?> After this, you will have array $res which will be something like: Array( [397] => 2, [1314] => 1 ) Then, if you really need the format specified, you can use something like: <? $final = Array(); foreach ($res as $source=>$value) $final[] = Array( 'source' => $source, 'value' => $value ); ?>
This code will do the counting and produce a $new array as described in your example. $data = array( array('source' => 397, 'value' => 1), array('source' => 397, 'value' => 1), array('source' => 1314, 'value' => 1), ); $new = array(); foreach ($data as $item) { $source = $item['source']; if (isset($new[$source])) $new[$source]['value'] += $item['value']; else $new[$source] = $item; } $new = array_values($new);
PHP has a function called array_count_values for that. May be you can use it Example: <?php $array = array(1, "hello", 1, "world", "hello"); print_r(array_count_values($array)); ?> Output: Array ( [1] => 2 [hello] => 2 [world] => 1 )
array grouping which contains same fields
I have an array which contains following values. array( 'dates' => array( (int) 0 => '2013-04-22', (int) 1 => '2013-04-23', ), 'publisherName' => array( (int) 0 => 'Comp1', (int) 1 => 'Comp2', ), 'loaded' => array( (int) 0 => (int) 2189, (int) 1 => (int) 37, ), 'clicks' => array( (int) 0 => (int) 0, (int) 1 => (int) 0, ), 'ctr' => array( (int) 0 => (int) 0, (int) 1 => (int) 0, ) ) What I want to produce is getting company based data on different dates like the array below. How am I able to create an array which is like; array ( '2013-04-22'=>array( 'publisherName'=>'Comp1', 'loaded'=>2189, 'clicks'=>0, 'ctr'=>0), '2013-04-23'=>array( 'publisherName'=>'Comp2', 'loaded'=>37, 'clicks'=>0, 'ctr'=>0) ... ) Which contains daily stats but which comes from publishername field. Any ideas?
Here's an example that doesn't rely on any keys, the only requirement is that date is the first array in your original array: // $array is your original array $dates = array_shift($array); $output = array(); foreach ($array as $k => $a) { foreach ($a as $i => $v) { $output[$i][$k] = $v; } } $output = array_combine($dates, $output);
Let the initial array be $a and the desired array $b; Code: $b = array(); $count = 0; foreach($a['dates'] as $date) { $b[$date] = array( 'name' => $a['publisherName'][$count], 'loaded'=> $a['loaded'][$count], 'clicks'=> $a['clicks'][$count], 'ctr'=> $a['ctr'][$count] ); $count++; } Result: Array ( [2013-04-22] => Array ( [name] => Comp1 [loaded] => 2189 [clicks] => 0 [ctr] => 0 ) [2013-04-23] => Array ( [name] => Comp2 [loaded] => 37 [clicks] => 0 [ctr] => 0 ) )
<?php $data = $yourarray; $new = array(); foreach($data['dates'] as $key=>$value) { $new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]); } echo '<pre>'; print_r($new); ?>
$newArr = array(); foreach($data['dates'] as $key=>$value) { $new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]); } echo '<pre>'; print_r($newArr);
$new_arr = your array; $finalArray = array(); foreach($new_arr['dates'] as $key => $value){ $finalArray[$value] = array( 'publisherName'=>$new_arr['publisherName'][$key], 'loaded'=>$new_arr['loaded'][$key], 'clicks'=>$new_arr['clicks'][$key], 'ctr'=>$new_arr['ctr'][$key] ); } Output : Array ( [2013-04-22] => Array ( [publisherName] => Comp1 [loaded] => 2189 [clicks] => 0 [ctr] => 0 ) [2013-04-23] => Array ( [publisherName] => Comp2 [loaded] => 37 [clicks] => 0 [ctr] => 0 ) )
PHP - count my quantity item in session array
How to count the item-qty and current code :- $q = $_POST['item-qty']; $i = count($q); $k = 0; while ($k < $i) { $select = 'SELECT * FROM location'; $query = $db->rq($select); $price = $db->fetch($query); if ($_POST['item-qty'][$k] < 3) { $get = $price['normal_price']; $price = $get * $_POST['item-qty'][$k]; $_SESSION['order'][$_POST['item-id'][$k]] = array( "item-id" => $_POST['item-id'][$k], "item-qty" => $_POST['item-qty'][$k], "item-name" => $_POST['item-name'][$k], "item-price" => $price, ); } else { $get = $price['member_price']; $price = $get * $_POST['item-qty'][$k]; $_SESSION['order'][$_POST['item-id'][$k]] = array( "item-id" => $_POST['item-id'][$k], "item-qty" => $_POST['item-qty'][$k], "item-name" => $_POST['item-name'][$k], "item-price" => $price, ); } } here the array output Array ( [order] => Array ( [1] => Array ( [item-id] => 1 [item-qty] => 1 [item-name] => Adidas [item-price] => 100 ) [2] => Array ( [item-id] => 2 [item-qty] => 1 [item-name] => Nike [item-price] => 150 ) ) ) Question : How to implement other code if item-qty (in all array) is greater than or equal to 3 will use $price['member_price'] let me know :)
I'm guessing you meant the total item-qty of everything? $qty_sum = 0 foreach($_SESSION['order'] as $order){ $qty_sum += $order['item-qty']; }