I have this foreach loop nested inside a while loop:
foreach ($_SESSION['arrayPoints'] as $value) {
echo $value;
}
This outputs the all the values stored in the array, 543216 as shown below, but as the foreach loop is in the while loop, 543216 is repeated until there are no more rows:
I need the foreach loop to output each value in the array separately for each row.
So for example (using the image above and the array values of 543216):
I need the output to show 5 for the first row, then 4 for the second row, 3 for the 3rd row, 2 fourth row and so on (as shown below).
Dave Jackson |TEXT BOX| |DROP-DOWN| 5
Bobby Brown |TEXT BOX| |DROP-DOWN| 4
Daniel Grey |TEXT BOX| |DROP-DOWN| 3
Richard Green |TEXT BOX| |DROP-DOWN| 2
David Bolt |TEXT BOX| |DROP-DOWN| 1
Jason Moore |TEXT BOX| |DROP-DOWN| 6
How can this be done?
EDIT:
print_r($_SESSION['arrayPoints']);
outputs:
Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 2 [4] => 1 [5] => 6 )
-The While loop: while ($data = mysqli_fetch_assoc($result)) { is a query fetch action.
-Each row shown in the image above is in order with each number in the array. So first row = 5 , second row = 4 and so on.
Thanks in advance.
Your current code says that you want to print every number for each data fetched from DB, which is not what you want. You want a single number printed for each data. So forget the foreach. What you need is:
echo $_SESSION['arrayPoints'][$i];
where $i is the current row being fetched (you need to set and increment it).
Related
How can i convert result from Database to somehow multidimensional array?
Example:
Records of users in DB
I tried coding something like this(Not code but illustration):
1 user => John Adams
2 users => Peter Greenman and John Adams
3 users => Sarah Josh,Peter Greenman and John Adams
50 users => Sarah Josh,Peter Greenman and 48 others.
How can i display records with the number of users in the DB just like i stated above
Thanks
declare array outside loop and store database value inside loop
$names = array();
while($stmt->fetch()){$names[]= database row;}
print_r($name);
I have a table named 'traffic' and two columns 'line' (there are over 50 lines) and 'vehicle' (over 300). This table contains a list of all of the lines and the vehicle they depend on. Each line has several vehicles and I need to group them (vehicles) in columns by line.
line | vehicle
______________________
line_a | veh1
line_a | veh12
line_a | veh123
line_b | veh14
line_b | veh15
line_b | veh16
line_c | veh17
line_c | veh18
Expected Output
line_a | line_b | line_c | .... to line 50
______________________________________
veh1 veh14 veh17
veh12 veh15 veh18
veh123 veh16
Any way to do this?
load all to php arrays
iterate once and collect unique lines via $helparr[$line]=array(), but also collect values per line array_push($helparr[$line],$vehicle)
iterate again: for all $helparr (output $line, for all $helparr[$line] (output $vehicle))
...guess, half of your coding work is done now...
first of all you need to fetch all the records from your database,
here i'm using a demo array $input just for example, you do not need this array when you are fetching from database, you can start from foreach
<?php
$input = array(0=>array('line'=>'line_a','vehicle'=>'veh1'),
1=>array('line'=>'line_b','vehicle'=>'veh2'),
2=>array('line'=>'line_c','vehicle'=>'veh6'),
3=>array('line'=>'line_a','vehicle'=>'veh5'),
4=>array('line'=>'line_c','vehicle'=>'veh9')
);
$newArr =array();
foreach($input as $key =>$val)
{
$newArr[$val['line']][] = $val['vehicle'];
}
echo "<pre>"; print_r($newArr);
?>
your $newArr looks like this
Array
(
[line_a] => Array
(
[0] => veh1
[1] => veh5
)
[line_b] => Array
(
[0] => veh2
)
[line_c] => Array
(
[0] => veh6
[1] => veh9
)
)
your vehicles are sorted according to their respective lines and you can use $newArray to print according to your need.
I hope this will work for you!
I am building a badges app in CodeIgniter using a MySQL database. There are 3 types of badges: level 1, level 2 and level 3 badges.
Each level 2 badge is awarded once you get all the required level 1 badges and each level 3 badge is awarded once you get all the required level 1 and level 2 badges. There is also a level 3 badge that requires getting 4 level 3 badges.
I have the following database table for my badges relations:
badge_id - children_id
1 - 20
1 - 25
20 - 40
20 - 45
26 - 40
25 - 39
40 - 50
I need a function that returns me all the ancestors of a badge.
For example, if the function recieved the argument 50 it would return: 40, 26, 20, 1. Any ideas?
If you want to handle it by pure mysql, you need to declare a mysql stored procedure which has some depth limitations.
Instead of that I would recommend using a simple recursive PHP function to return what you need:
function get_ancestors($child_id, $badges)
{
$found = array();
foreach($badges as $k => $row){
if ($row['children_id'] == $child_id){
$found = array_merge($found, get_ancestors($row['badge_id'], $badges));
$found[] = $row['badge_id'];
}
}
return $found;
}
The $badges variable holds all the rows from your badges relations table, just simply read it out from the database.
Based on the example you gave, for the following call
get_ancestors(50, $badges);
the output would be:
Array
(
[0] => 1
[1] => 20
[2] => 26
[3] => 40
)
I have CSV list with team names in it (this is one row):
Teams
Red
Green | Red | Yellow
Red
Yellow | Green | Yellow
Red
Green
Yellow | Yellow | Red
Red
Green
Red
Yellow
I need to be able to count each individual color and count top 4
and number of times they appear.
How can I do that? For example if i try using:
$teams = file('count.csv');
$count[] = (array_count_values($colors));
print_r($count);
I get:
Array ( [0] => Array ( [Teams ] => 1
[Red ] => 5 [Green | Red | Yellow ] => 1 [Yellow | Green | Yellow ] => 1 [Green ] => 2 [Yellow | Yellow | Red ] => 1 [Yellow] => 1 ) )
Which is not very useful. And afrewards how could I compare the valies to one another to get top 4?
Any tricks known how to make this happen? Thank you in advance!
OK another try:
$inputfile = 'count.csv';
$inputHandle = fopen($inputfile, "r");
while (($data = fgetcsv($inputHandle, 1024, ",")) !== FALSE) {
$teams = $data[0];
$teams = explode('|', $teams);
}
$count[] = (array_count_values($teams));
print("<pre>".print_r($count, true)."</pre>");
I get
Array
(
[0] => Array
(
[Yellow] => 1
)
)
What am I doing wrong?
If I understand correctly, you've got one row of data, with | as the delimiter.
Here's a very crude quick'n'dirty solution.
What I would do is first use strpos() and substr() to get the colors, and use a switch-case to increment counters for each color. To get the top 4, create an array to hold four strings, insert the first four colors, and then compare the remaining colors to each value in the array, overwriting values in the array if the color you're comparing has a higher count than any value in the array.
This sounds a bit confusing, so let me know if you'd like me to write some sample code demonstrating this.
Original data looks like this: banners/ad_1.png | Banner ad 1 | 1
Here is an array using the print_r function on it:
Array ( [0] => banners/ad_1.png Banner ad 1 1
[1] => banners/ad_2.png Banner ad 2 2
[2] => banners/ad_3.png Banner ad 3 3 )
This is after exploding it with a | delimiter, so it's separated by img src, alt text, num times viewed.
Is there a way I can return the banner information by num times viewed, max or min?
I have been playing with min, max, array_values, array_keys, array_multisort.. I can't figure it out.
Thanks!
This should work, provided this array doesn't get so big that it eats up significant chunks of memory:
<?php
$array = array(
'banners/ad_1.png | Banner ad 1 | 1',
'banners/ad_2.png | Banner ad 2 | 2',
'banners/ad_3.png | Banner ad 3 | 3'
);
$sort = array();
foreach ($array as $row)
{
$row = explode(" | ", $row); // split up string into a format we can deal with
// use a unique index so we can keep track of association
$idx = trim($row[0]);
$sort[$idx] = trim($row[2]);
}
arsort($sort); // sorts by value in descending order, keeps index association
print_r($sort);
/*
Should be:
Array(
'banners/ad_3.png' => 3,
'banners/ad_2.png' => 2,
'banners/ad_1.png' => 1
)
*/
Here's some documentation on the arsort function I used.