How to iterate multiple loops simultaneously in Laravel (PHP) - php

I want to iterate 5 Foreach Loops At Same Time How to achieve so ? Any Answers ?
foreach ($campaign_match2 as $c2) {
echo "<br>Mailing 2";
}
foreach ($campaign_match3 as $c2) {
echo "<br>Mailing 3";
}
foreach ($campaign_match4 as $c2) {
echo "<br>Mailing 4";
}
foreach ($campaign_match5 as $c2) {
echo "<br>Mailing 5";
}
Output :-
Mailing 1
Mailing 2
Mailing 3
Mailing 4
Mailing 5
700 times simultanously

Related

PHP foreach targeting specific item

I got a DB query that pulls out three items randomly from several categories. I want to show "Selected" if one of the items is from a specific category. Right now if one of the items is from that specific category the "Selected" is showing on all three items. How can I target the specific item only?
foreach ( $rows as $row ) {
if ($row->catid == 56) {
echo "Selected";
}
Your question isn't very clear, but I suspect you're trying to add the attribute to an option. You need to do this in the same loop that echoes the option.
foreach ($rows as $index => $row) {
$selected = $row->catid == 56 ? "SELECTED" : "";
$itemnum = $index + 1;
echo "Item $itemnum - Category $row->catid $selected | ";
}
foreach ( $rows as $row ) {
if ($row->catid == 56) {
echo "Selected";
break;
}
Not sure what u want accomplish but i guess u want to show only one selected so u need to break loop if item is found

display student details if absent continuously for 5 days

I have a problem displaying students details who are absent for 5 days continuously. I wrote a sql query like :
$res = $this->db->findAll("SELECT
student_id,attn_date,count(is_absent) as absent FROM
sg_students_attendance where is_absent=1 GROUP BY student_id ORDER BY
attn_date desc");
The condition which I applied is:
foreach($res as $rec)
{
$abs=$rec['absent'];
$abs1=5;
if($abs1==$abs)
{
echo "<tr>";
echo "<td>".$rec['student_id'];
echo "<td>".$rec['absent'];
echo "</tr>";
}
}
I am getting students details those who are absent for 5 days but not continuously. What will be the query for that ???
You can do this with PHP code
$res=$this->db->findAll("SELECT student_id,attn_date,is_absent as absent FROM sg_students_attendance where is_absent=1 ORDER BY student_id,attn_date desc");
PHP code
$count = 0;
foreach($res as $rec)
{
$abs=$rec['absent'];
if($abs == 1)
$count++;
else
$count = 0;
if($count == 5)
{
echo $rec['student_id'] . ' is absent for continuous 5 or more days<BR>';
$count = 0;
}
}

Recursive tree traversal with mysql through PHP

I am creating a questionnaire for a client that requires the questions to be organized by 3 layers of levels. I've successfully created the U.I. however I've been trying for the last 3 hours to pull data from a database in such a way that everything loads in the right place. The database is organized like so by the client so I have no control over it:
id description parentId
1 Level 1 0
2 Level 2 0
3 Level 1a 1
4 Level 1b 1
5 Level 1a1 3
I have found a similar question to mine on the site but when I attempted it's solution I got the following on repeat infinetly:
Code:
function makeList($par_id = 0) {
//your sql code here
$result = mysql_query("SELECT * FROM pB_test WHERE parentId = $par_id");
$pages = mysql_fetch_array( $result );
if (count($pages)) {
echo '<ul>';
foreach ($pages as $page) {
echo '<li>', $page['description'];
makeList($page['parentId']);
echo '</li>';
}
echo '</ul>';
}
}
makeList();
Output:
1
3
5
5
l
l
3
5
5
l
l
3
5
5
l
l
3
5
5
l
l
Does anyone know how to fix this and what the issue is exactly? Cheers
it's not good to call mysql server and fetch result each time
what if you have over 100 rows? or 200+
use this to query only once:
$result = mysql_query("SELECT * FROM test");
$arrs = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$arrs[] = $row;
}
function build_tree($arrs, $parent_id=0, $level=0) {
foreach ($arrs as $arr) {
if ($arr['parent_id'] == $parent_id) {
echo str_repeat("-", $level)." ".$arr['name']."<br />";
build_tree($arrs, $arr['id'], $level+1);
}
}
}
build_tree($arrs);
common example for table
id name parent_id
Do this recursivly:
function printChildQuestions($parentid) {
$sql="SELECT * FROM pB_test WHERE parentID=$parentid";
$result=mysql_query($sql);
$i=0;
while (true) {
$row=mysql_fetch_array($result);
if (!$row) break;
if ($i==0) echo "<ul>";
$i=1;
echo '<li>'.$row['id'].' '.$row['description'].' '.$row['parentId'].'</li>';
printChildQuestions($row['id']);
}
if ($i>0) echo '</ul>';
}
printChildQuestions(0);

php mysql - echo clear division after 3rd result

I echo results in DESC order from MySQL table with while loop. I've pagination system implemented already and its size is 9 records per page. The problem is, that if I do:
// ECHO CSS BREAK
if($row['id'] % 3 == 0){
echo '<li class="clear"></li>';
}
// SHOW VIDEOS
while($row = mysql_fetch_array($result)){
echo '<li>...echo code...</li>';
// problem = implement that echo css break in ASC order
}
Use a loop variable, e.g.
$i = 0;
Then instead of
if ($row['id'] % 3 == 0) {
do
if (++$i % 3 === 0) {
This makes sure it always happens are the third [sixth, ninth, ...] time.
You may want to get arbitrary rows from the database at another point in time, or shuffle the results -- relying on row IDs is not a good idea.
Is this what you are looking to implement?
// SHOW VIDEOS
while($row = mysql_fetch_array($result)){
if($row['id'] % 3 == 0){
echo '<li>...echo code...</li>';
echo '<li class="clear"></li>';
} else {
echo '<li>...echo code...</li>';
}
}

Delimit records PHP

If I have a while loop that retrieve records, I want to be able to delimit records by wrapping them after an amount of records while the loop is going, e.g.
(using a while loop):
Record 1
Record 2
Record 3
Record 4
Record 5
Record 6
Record 7
But I need to group records like this:
<div class="wrap">
Record 1
Record 2
Record 3
</div>
<div class="wrap">
Record 4
Record 5
Record 6
</div>
Record 7
So when it exceeds more than 3 it should wrap every 3 count.
$index = 0;
while (...) {
if ($index == 0) {
echo '<div class="wrap">';
} elseif (($index % 3) == 0) {
echo '</div><div class="wrap">';
}
// Output your stuff
$index++;
}
if ($index != 0) {
echo '</div>';
}
<?php
// Dummy data
$records = array('1','2','3','4','5','6','7');
// While we have at least 3 records, group them
while (count($records) > 3) {
$subs = array_splice($records,0,3);
print '<div class="wrap">'.implode(PHP_EOL, $subs).'</div>';
}
// Dump the rest
print implode(PHP_EOL, $records)
?>

Categories