array isn't combining with loop in php - php

i've a array problem couldn't just solve it:
here's the code that is already in a foreach loop. i'm getting the $row->class_id value from this loop. it works fine and get me the total students row within an array and i preserved it in $total_student_of_this_class variable. i used another foreach loop for storing the result of first loop and then second loop and so on. but this loop gives only first loop result.
i need to combine the all array of total iteration of the loop.
$total_student_of_this_class =
$this->db->select('student_id')->where('class_id',
$row->class_id)->get('student')->result_array();
echo count($total_student_of_this_class);
// prints 2 in the first row of the output table and 5 in the second
row for me.
$total_student = array();
foreach ($total_student_of_this_class as $tt)
{
$total_student[] = $tt;
}
echo '<pre>';
print_r($total_student_of_this_class);
echo '</pre>'
echo count($total_student);
// prints only 2 outside the loop (not 7)
pls someone help me.

The problem here is seems to be with $total_student = array();: initializing array inside an other loop it always starts it from zero values array, so you get only results from one iteration inside it and doesn't collect all datas.
You can also look at array_merge php function.

Related

foreach loop modifies an array without I want

I don't understand what happens, I have an array of associative arrays, and I want to iterate through it so I made a foreach loop but this loop modified my initial array and I don't know why, loot at the code :
1st case
var_dump($modules);
$tab = $modules;
foreach ($tab as $module){
var_dump($module);
}
The result :
On the left, is the initial array and at the right : the different values of the array. As you can see, it doesn't show the second one item but only the one. If I show the $tab inside of the loop, it is just an array of 2 items containing twice copies of the "jojo" item .
2nd case
However, if I write this :
for($i=0;$i<count($tab);$i++){
var_dump($tab[$i]);
}
It works fine as I want
and if I execute the for loop after the foreach loop the result is the same as the 1st case. Do you knwo why ?

How to get each value in a column of database in php

I don't have enough knowledge about this criteria:
I want to loop inside the for each loop to get all values in a particular column.
For example: I got the values from DB through get_result and store the result in $results.
After that use:
for each($results as $result)
❴
$output = $result->message
❵
Where message is a column in DB.
I want to loop over all the messages instead of storing last one by replacing.
Can you please give me suggestions on how to loop inside for each?
Try this:
$output[] = $result ->message;
Now $output will contain all messages on index 0, 1, 2 ...
You are facing the issue because:
$output=$result ->message;
the above line is present inside the loop, and each new iteration onerride the old value.
Well if you just looking for foreach inside foreach then you can try the following.
<?php
foreach($results as $result){
$output=$result->message;
foreach($output as $messages){
echo $messages;
}
}
?>
You don't need to put the message into another variable. You can do whatever you need to do inside the loop. For example, if you are displaying the messages, you can get it done inside the loop:
foreach ($results AS $result) {
echo $results->message . "<br>";
}

I want a query result using codeigniter

i have an array result. i want to print 2 rows(product data) in first page.
next 2 rows in second page and so on. if anybody knows this,please help me to solve it
my array
$data['product_list']
foreach($data['product_list'] as $dat)
{
echo $dat->prd_id;
echo $dat->prd_name;
}
You are doing a foreach loop on an associative array and then trying to access the the contents as objects by using ->. I can only given assumption of what you might be doing. If your array is already populated with a name and id like you have described in your foreach loop this is how you would access the contents in the loop:
foreach($data['product_list'] as $dat)
{
echo $dat['prd_id'];
echo $dat['prd_name'];
}
That is how you would print out the contents providing you had the data stored in your array like so:
$data['product_list'][0] = array('prd_id'=>'id0','prd_name'=>'name0');
$data['product_list'][1] = array('prd_id'=>'id1','prd_name'=>'name1');
$data['product_list'][2] = array('prd_id'=>'id2','prd_name'=>'name2');
Better you try with array_slice();
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>

What is the method to execute PHP array one by one after insert into db for all arrays?

$countsara= array(array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"david"), array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"kaser"),array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"albert"),array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"stephen"));
foreach ($countsara as $tommykey=>$tommyval) {
foreach ($tommyval as $tommyvalkey=>$tommyvalval) {
echo $tommyvalkey.' - '.$tommyvalval;
echo '<br>';
echo 'test';
}
}
For example, there are 100 arrays. I want to get the first array values and insert into db, then 2nd and then until 100th array values.
I can able to insert the value into db but the issue is, if there are 100 arrays. The foreach is executing for 100 times. So the insert is happening for 100 times for a same id on a same record row. Which is a re-insert not a duplicate.
If there are totally 100 arrays in $tommyval. As usual, the test is being executed for 10 times. I am going to do an operation in the place of echo test; So I don't want to repeat the same operation 100 times.
How to avoid this in PHP foreach array repetition at the same time I want all the array to be executed?
$countsara= array(array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"david"), array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"kaser"),array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"albert"),array("a"=>"hello","b"=>"how","c"=>"are","d"=>"you","e"=>"stephen"));
foreach ($countsara as $tommykey=>$tommyval) {
foreach ($tommyval as $tommyvalkey=>$tommyvalval) {
echo $tommyvalkey.' - '.$tommyvalval;
echo '<br>';
}
echo 'test';
}
The change that I made was placed the echo 'test'; after the completion of second foreach. I got what I wanted. I executed only once and so as the insertion into db also happens only once.
if you mean that you want to execute the insert portion of the code 10 times but //echo 'test'; portion should only be executed once, try setting a flag which will only let the //echo part once and stop it from executing with every loop

For-Each Loop Misses First Index When Multiple Indices Exist

I've got the below PHP which works great when the array of locations only have 1 value, but when it has any number more than 1, the information on the first location keeps printing out blank. The array is set by calling explode on a comma-delimited list. When I set the array manually immediately before the loop, it works great! Yet not when I use explode, even though I printed the array and can confirm it's getting set up just as I expect (with the correct 0 index). The code I have is:
echo "<ul>";
foreach ($locations as &$value)
{
$locationDetails = mysql_query("SELECT id, name FROM locations WHERE id='$value'");
$locationDetailRow = mysql_fetch_assoc($locationDetails);
echo "<li>".$locationDetailRow['name']."</li>";
}
unset($value);
echo "</ul>";
I've confirmed that the query does not fail, that $value is what I expect it to be, and that the name does exist for the first $value in every case.
Any reason this should be failing on the first case when there are multiple locations?

Categories