Reputation: 45
There are multiple activities(with their unique activityid's) for a campaign(with unique campaignid).For a campaignid, activityid's begin from 1 and go on and as the campaignid changes, activityid's begin from 1 again.
What I want to do is have a different table for a particular campaignid.
What is happening right now in my code is that for every table(for every campaignid), only the first campaignid's all the activityids are being displayed in all the tables if I use break 1;.If I dont use break 1;,then all the activityids( related to all the campaignids) are being displayed in all the tables.What I want is to have first campaignid's all the activityids in the first table, second campaignid's all the activityids in the second table, and so on.Any suggestions on how to achieve this?
MYSQL columns:
Table campaigns:
Table campaignactivities:
PHP:
<div class="table-responsive" >
<?php
//print the rows
$q1=mysqli_query($con,"select * from clientusers where Clientemail='".$_SESSION["Email"]."'");
$row1=mysqli_fetch_assoc($q1);
$id1=$row1['Clientid'];
$q2=mysqli_query($con,"select * from campaigns where Clientid='$id1'");
$row2=mysqli_fetch_assoc($q2);
$id2=$row2['Clientid'];
$colNames = array();
$data = array();
$data[] = $row2;
while($row2 = mysqli_fetch_assoc($q2))
{
$data[] = $row2;
}
$data=array_reverse($data);
$colNames = array_keys(reset($data));
$colNames =array($colNames[2],$colNames[6],$colNames[1]);
foreach($data as $val)
{
echo "<tr>";
?><div class="well">
<?php
$q3=mysqli_query($con,"select * from campaignactivities where Clientid='$id2'");
$row3=mysqli_fetch_assoc($q3);
$colNames2 = array();
$data2 = array();
$data2[] = $row3;
while($row3 = mysqli_fetch_assoc($q3))
{
$data2[] = $row3;
}
$colNames2 = array_keys(reset($data2));
$colNames2 =array($colNames2[5],$colNames2[6],$colNames2[7],$colNames2[8],$colNames2[9],$colNames2[10],$colNames2[11],
$colNames2[12],$colNames2[13],$colNames2[14]);
?>
<table class="table table-condensed table-striped table-bordered table-hover no-margin well">
<tbody>
<tr>
<?php
$temp=1;
foreach($data2 as $val2)
{
echo "<tr>";
if ($temp>(int)$val2[$colNames2[0]]) {$temp=1;break 1;}
else {$temp=(int)$val2[$colNames2[0]]; echo "</tr>";};
foreach($colNames2 as $colName2)
{
echo "<td>".$val2[$colName2]."</td>";
}
}
?>
</tr>
</tbody>
</table><?php echo "</div><br>";} ?>
</div>
Upvotes: 1
Views: 76
Reputation: 21513
If I understand you correctly you have groups of data being returned by a single SQL query. For each group of data (where the first item in that group having 1 stored in the field whose name is stored in $colNames2 ) you want to put out a separate HTML table.
Normally with this kind of issue you check the field that you want to trigger the change, and when a change is needed you output the end of one table and the start if the next. You output the first start table before the loop and the last end table after the loop.
You could use a class for the tables to do this. Create a new object from the class before the loop, each time the group changes unset the object and create a new one, and unset the object after the loop. In the class put out the start of the table in the constructor and the end of the table in the destructor. Something like this:-
<div class="table-responsive" >
<?php
//print the rows
$q1=mysqli_query($con,"select * from clientusers where Clientemail='".$_SESSION["Email"]."'");
$row1=mysqli_fetch_assoc($q1);
$id1=$row1['Clientid'];
$q2=mysqli_query($con,"select * from campaigns where Clientid='$id1'");
$row2=mysqli_fetch_assoc($q2);
$id2=$row2['Clientid'];
$colNames = array();
$data = array();
$data[] = $row2;
while($row2 = mysqli_fetch_assoc($q2))
{
$data[] = $row2;
}
$data=array_reverse($data);
$colNames = array_keys(reset($data));
$colNames =array($colNames[2],$colNames[6],$colNames[1]);
foreach($data as $val)
{
echo "<tr>";
?><div class="well">
<?php
$q3=mysqli_query($con,"select * from campaignactivities where Clientid='$id2'");
$row3=mysqli_fetch_assoc($q3);
$colNames2 = array();
$data2 = array();
$data2[] = $row3;
while($row3 = mysqli_fetch_assoc($q3))
{
$data2[] = $row3;
}
$colNames2 = array_keys(reset($data2));
$colNames2 =array($colNames2[5],$colNames2[6],$colNames2[7],$colNames2[8],$colNames2[9],$colNames2[10],$colNames2[11],
$colNames2[12],$colNames2[13],$colNames2[14]);
$aTable = new aTable();
$temp=1;
foreach($data2 as $val2)
{
if ($temp>(int)$val2[$colNames2[0]])
{
$temp = 1;
unset($aTable);
$aTable = new aTable();
}
$aTable->write_line($colNames2, $val2)
}
unset($aTable);
echo "</div><br>";}
?>
</div>
<?php
class aTable
{
function __construct()
{
echo "<table class='table table-condensed table-striped table-bordered table-hover no-margin well'> <tbody> <tr>";
}
function __destruct()
{
echo "</tr> </tbody> </table>";
}
function write_line($colNames2, $val2)
{
foreach($colNames2 as $colName2)
{
echo "<td>".$val2[$colName2]."</td>";
}
}
}
?>
Upvotes: 1