Checking the value of all a loops value - php

I have this loop in PHP that echoes out a table of results,
<table cellspacing="0" cellpadding="3">
<tr>
<td><b>Content Image Title</b></td>
<td><b>Content Image Type</b></td>
<td><b>Headline Image</b></td>
<td><b>Content Image Belongs To</b></td>
<td><b>Date Created</b></td>
<!--<td><b>Uploaded By</b></td>-->
</tr>
<?php $colours = array("#f9f9f9", "#f3f3f3"); $count = 0;?>
<?php foreach ($allContentImages as $contentImages) : ?>
<tr bgcolor="<?php echo $colours[$count++ % count($colours)];?>">
<td><?php echo "<a href='#' class='screenshot' rel='/media/uploads/$contentImages[categoryId]/$contentImages[contentImageName]'>".$contentImages['contentImageName']; ?></td>
<td><?php echo $contentImages['contentImageType']; ?></td>
<td><?php if($contentImages['isHeadlineImage'] == 1){ echo "Y";}else{echo "N";} ?></td>
<td><?php echo $contentImages['contentTitle'] ?></td>
<td><?php echo date("d-m-Y", $contentImages['contentImageDateUploaded']); ?></td>
<td align="left"><a class="delete" href="<?php echo base_url();?>dashboard/deleteContentImage/<?php echo $contentImages['contentImageId'];?>"><img src="/media/images/icons/cancel.png" alt="Delete A Category"/></a></td>
</tr>
<?php
if($contentImages['isHeadlineImage'] == '0') {
echo "<tr bgcolor='red'>";
echo "<td><p>You need to assign a headline image</p></td>";
echo "</tr>";
?>
<?php endforeach; ?>
</table>
I need to check for each content piece with the same title that there is headline image, and if not then echo a new row that is red...but all I get is a new row every time there is an image that is not a headline image. Can anyone help me? I don't mind using javascript if that helps to match the values of the td's? But obviously my attempt is not correct.

It sounds like what you want to do has to be precalculated, since the foreach loop can not know about future content.
Maybe something like this before your foreach statement:
$contentHasHeadlineImage = array();
foreach ($allContentImages as $contentImages) {
if ( $contentImages['isHeadlineImage'] == 1)
$contentHasHeadlineImage[ $contentImages['contentTitle'] ] = true;
}
And then you can use
if (array_key_exists($contentImages['contentTitle'], $contentHasHeadlineImage)) {
// Has headline image...
}
to to verify if a certain title has a headline.

Try my code below:
<table cellspacing="0" cellpadding="3">
<tr>
<td><b>Content Image Title</b></td>
<td><b>Content Image Type</b></td>
<td><b>Headline Image</b></td>
<td><b>Content Image Belongs To</b></td>
<td><b>Date Created</b></td>
<td>Action</td>
</tr>
<?php
$colours = array("#f9f9f9", "#f3f3f3");
$num_colours = count($colours);
$i = 0;
?>
<?php foreach ($allContentImages as $row) : ?>
<tr bgcolor="<?php echo $colours[($i++) % $num_colours]; ?>">
<td><a href="#" class="screenshot"
rel="/media/uploads/<?php echo $row['categoryId']; ?>/<?php echo $row['contentImageName']; ?>">
<?php echo $row['contentImageName']; ?>
</a></td>
<td><?php echo $row['contentImageType']; ?></td>
<td><?php echo $row['isHeadlineImage'] == 1 ? "Y" : "N"; ?></td>
<td><?php echo $row['contentTitle']; ?></td>
<td><?php echo date("d-m-Y", $row['contentImageDateUploaded']); ?></td>
<td align="left"><a class="delete"
href="<?php echo base_url(); ?>dashboard/deleteContentImage/<?php echo $row['contentImageId'];?>">
<img src="/media/images/icons/cancel.png" alt="Delete A Category" />
</a></td>
</tr>
<?php if ( $row['isHeadlineImage'] == 0 ): ?>
<tr bgcolor="red">
<td colspan="6"><p>You need to assign a headline image</p></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</table>
While tidying your code, I found an unclosed a href in the first column. For the special extra row, I add colspan. I assume $row['isHeadlineImage'] have value only 1 or 0 (integer or boolean).
I also found out that you access array key without using quote. It can become potential bug in the future.
Write the code in tidy indentation and consistent when using block or echoing the php variable will help you found the bug quickly. Also, inspect the result in browser using Firebug in firefox or Web Inspector in Safari and Google Chrome to see if the page structure is like you want to have, all the tag is balanced and closed in right place.

Related

How to make sql image clickable on display board

I am trying to make the fetched images on a sql database clickable. They display fine but no-one can click them or download them and I'd like them to be able to. The script is below.
<?php
include_once 'connect.php';
$result = mysqli_query($conn,"SELECT * FROM Leaderboard");
?>
<?php
if (mysqli_num_rows($result) > 0) {
?>
<p>Leaderboard and guest photos as of October 20, 2020. Our apologies to our prior guests.</p>
<table class='table table-bordered table-striped'>
<tr>
<td>Date</td>
<td>Photo</td>
<td>Team Name</td>
<td># of Players</td>
<td>Escape Room</td>
<td>Time</td>
<td>Success</td>
<td>Game Master</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["date"]; ?></td>
<td><?php echo "<img src='/teamphotos/".$row['photo']." ' width='250' height='200'>"; ?>. </td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["players"]; ?></td>
<td><?php echo $row["room"]; ?></td>
<td><?php echo $row["time"]; ?></td>
<td><?php echo $row["win"]; ?></td>
<td><?php echo $row["master"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
Probably you want that if the user left clicks the image, a download of that image starts. For that you only need to wrap an anchor tag <a> around the <img> with the attributes src and download.
<td><?php echo ''; ?></td>
This is related to HTML and not to PHP. Here you can read more about it:
https://www.w3schools.com/howto/howto_html_download_link.asp

Retrieve Server Data from a mysql db

I have question about how i shall do this.
First i have taken out a list from a mysql database which.
What I want to do is to be able to click on one of these items in the list so that I can get the underlying facts for this particular item.
Now i want to use this as a link to get one row from the database.
The database is done and the list i can see in a website but i dont get the last part to work
show_all_symbol.php
//This show the list
<?php
include 'connect.php';
$sql = "SELECT * FROM t1_symbolism ";
$q = $pdoconn->query($sql);
$q ->setfetchmode(PDO::FETCH_ASSOC);
?>
<table>
<head>
<tr><th>Symbol</th><th>Alternativtnamn</th> <th>Symboltyp</th></tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<td><img src="<?php echo "/symbol/",$linksbild,($row['symb_type']),"/",($row['symb_pic']); ?>"/></td>
<tr>
<td> <a id="leftside_meny" href= <?php echo "/symbol/show_symbol.php";?> target= "texten"> <?php echo ($row['symb_name']); ?></a></td>
<td><?php echo ($row['symb_name_other']); ?></td>
<td><?php echo ($row['symb_type']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
show_symbol.php
<body>
<?php
$funamn = trim($_POST['symbname']);
include 'connect.php';
$sqlanswer = $pdoconn->query(" SELECT * from t1_symbolism where symb_name '$funamn'");
$row = $sqlanswer->fetch();
?>
<table >
<thead>
<tr><th>Symbol</th><th>Symbolnamn</th><th>Alternativtnamn</th> <th>Symboltyp</th><th>Fakta</th></tr>
</thead>
<tr>
<td><img src="<?php echo "/symbol/",$linksbild,($row['symb_type']),"/",($row['symb_pic']); ?>" width="250" height="250"/></td>
<td><?php echo ($row['symb_name']); ?></td>
<td><?php echo ($row['symb_name_other']); ?></td>
<td><?php echo ($row['symb_type']); ?></td>
<td><?php echo ($row['symb_history']); ?></td>
</tr>
</table>
</body>
What i want to do is to use this part as Use this part as a clickable link so I can view a record in the table.
<td> <a id="leftside_meny" href= <?php echo "/symbol/show_symbol.php";?> target= "texten"> <?php echo ($row['symb_name']); ?></a></td>
Thank in the advance!
it looks like you are looking for a way to pass this value $row['symb_name'] to this value $_POST['symbname']. Here's what i think you should try :
in your show_all_symbol.php
<td>
<a id="leftside_meny" href="<?php echo "/symbol/show_symbol.php?symb_name=" . $row['symb_name'];?>" target="texten">
<?php echo ($row['symb_name']); ?>
</a>
</td>
in your show_symbol.php
$funamn = trim($_GET['symb_name']);
If you want to use $_POST then you will have to either figure a way to make a form out of each clickable element and configure it to make a POST or do it using javascript or jquery.

Cannot connect rows in html with php (logical error display)

The html table rows aren't displayed properly. I want to combine the two while loops in the table row but sadly the Update and Delete button are not arranged properly.
here's my code I used two queries that's why it has two while loops
$sql_sel=mysql_query("SELECT students.stud_id, students.fname, students.lname, students.gender, subjects.sub_code, subjects.subject_name FROM students, enrollments, subjects WHERE students.stud_id = enrollments.stud_id and subjects.sub_code = enrollments.sub_code");
$sql_sel1=mysql_query("SElECT * FROM enrollments");
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<?php
while($row=mysql_fetch_array($sql_sel1)) //for the second query
{ <!-------The Update and Delete Button are not displayed properly------>
?>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
<?php
}
?>
</tr>
<?php
}
?>
Here is the visual scenario of the problem:
The desired output must be like this:
In table header use colspan='4' for the last column.
Also be sure that you fill the table with empty columns, where you don't have information to fill with.
Edit 1:
Sorry, I haven't seen what's really the problem was. Here should be the working code:
while($row=mysql_fetch_array($sql_sel))
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<!-- You were already in a while loop -->
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
As you can see you were already in a while loop, and the second one was unnecesary.
Edit 2:
There is a single SQL query now:
<?php
// UPDATED SQL QUERY
$sql_sel = mysql_query("SELECT students.stud_id, students.fname, students.lname, students.gender, subjects.sub_code, subjects.subject_name, enrollments.enroll_num
FROM students, enrollments, subjects
WHERE students.stud_id = enrollments.stud_id and subjects.sub_code = enrollments.sub_code");
while($row = mysql_fetch_array($sql_sel)){
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
Why it didn't worked?
You were reading the whole informations for every student. Then you were reading the whole informations in enrollments table.
You started writing the first row with student information, and inside it you told the server to start writing all the information he had regarding enrollments (without even be linked to that student's id).
When the server reached the second row, all the information available for enrollments were depleted.
Now you have them linked in your first query. Please ask in comments if you need further explanations.
Try this,
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.gender, sub.sub_code, sub.subject_name, ets.enroll_num
FROM students sts
JOIN enrollements ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.sub_code = ets.sub_code)
GROUP BY sts.stud_id, sub.sub_code";
$sql_sel=mysql_query($sql);
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['stud_id'];?></td>
<td><?php echo $row['fname']." ".$row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['sub_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center"><img src="picture/update.png" /></td>
<td align="center"><img src="picture/delete.png" /></td>
</tr>
<?php
}
?>
I have added enroll_num column in select, And you dont need two queries for this. One query with proper join will be fine.

get unique ID's of rows in php foreach loop for JQuery

Good Afternoon,
I have 3 foreach loops on my page, the first gets the teams, the second gets each person in the team, and the third gets each unique reference to a task that the agent has completed. I have collected this data and it is being displayed fine. I now want to add some JQuery to it so it will hide the agents and references unless the relevant team or agent is clicked on.
So if I load the page everything will be hidden apart from the teams, when I click on a team it will show the agents, when I click on an agent it will show the references.
I am having trouble assigning unique ID's to each row and finding those in the JQuery script.
Here is my code...
<?php if($aForm['sTaskType'] !== 'CP' ){?>
<table style="width: 95%">
<tr>
<th>Area</th>
<th>Pass</th>
<th>Pass with feedback</th>
<th>Fail with Minors</th>
<th>Fail with Majors</th>
</tr>
<?php foreach ($aQualityTeamResults AS $iBusinessStreamId => $aTeamData) {
$aQualityAgentResults = $oRadiusQualityFns->GetQualityAgentResults($sDateFrom, $sDateTo, $sTaskType, $aTeamData['iBusinessStreamId']);?>
<tbody>
<tr class="TeamClick<?php echo $aTeamData['iBusinessStreamId'];?>">
<td><?php echo $aTeamData['sBusinessStream']?></td>
<td><?php echo $aTeamData['Pass']?></td>
<td><?php echo $aTeamData['Pass with Feedback']?></td>
<td><?php echo $aTeamData['Fail with Minors']?></td>
<td><?php echo $aTeamData['Fail with Majors']?></td>
</tr>
</tbody>
<?php foreach ($aQualityAgentResults AS $iUserId => $aAgentData) {
$aQualityPropertyResults = $oRadiusQualityFns->GetQualityPropertyResults($sDateFrom, $sDateTo, $sTaskType, $aAgentData['iBusinessStreamId'], $aAgentData['Agent']);
?>
<tbody>
<tr class="Agent<?php echo $iUserId]?>">
<td><?php echo $oRadiusUser->Get_User_Name($aAgentData['Agent']);?></td>
<td><?php echo $aAgentData['Pass'];?></td>
<td><?php echo $aAgentData['Pass with Feedback'];?></td>
<td><?php echo $aAgentData['Fail with Minors'];?></td>
<td><?php echo $aAgentData['Fail with Majors'];?></td>
</tr>
</tbody>
<?php foreach ($aQualityPropertyResults AS $iUserId => $aPropertyData) { ?>
<tbody>
<tr class="Property<?php echo $aPropertyData['iUserId'];?>">
<td colspan="2"><font color="black"><?php echo $aPropertyData['sPropertyCode']?></font></td>
<td colspan="3"><?php echo $aPropertyData['Result']?></td>
</tr>
</tbody>
<?php
}
}
}
?>
</table>
I have given each of the rows a unique class by adding in the unique identifier from the database. I just dont know how to find these within the Jquery script.
EDIT:
Maybe not explained myself properly, I would like help with how to set up this script but obviously a lot better.
<script language="javascript" type="text/javascript" >
$(document).ready(function() {
$('.Agent').hide;
$('.Property').hide;
$(document).on('click','.TeamClick',function(){
$('.Agent').toggle('show');
$('.Property').toggle('show');
});
});
</script>
But in this case it will show/hide all of the rows as they will all have the same id's, whereas now I have added on the unique id on the end with the php code in the class, I dont know how to call those as they all are called different classes.
So if I click on TeamClick1, it shows the actual rows for that team (Agent1), and not all of them. but obviously I cant type out all of the unique id's I just dont know how to get them from the php in JQuery.
<script language="javascript" type="text/javascript" >
$(document).ready(function() {
$('.Agent').hide;
$('.Property').hide;
$(document).on('click','.TeamClick(UNIQUE ID)',function(){
$('.Agent(UNIQUE ID)').toggle('show');
$('.Property(UNIQUE ID)').toggle('show');
});
});
Hope this makes sense.
I am hasty but it is like this
<?php if($aForm['sTaskType'] !== 'CP' ){?>
<table style="width: 95%">
<tr>
<th>Area</th>
<th>Pass</th>
<th>Pass with feedback</th>
<th>Fail with Minors</th>
<th>Fail with Majors</th>
</tr>
<?php foreach ($aQualityTeamResults AS $iBusinessStreamId => $aTeamData) {
$aQualityAgentResults = $oRadiusQualityFns->GetQualityAgentResults($sDateFrom, $sDateTo, $sTaskType, $aTeamData['iBusinessStreamId']);?>
<tbody>
<tr class="TeamClick<?php echo $iBusinessStreamId;?>">
<td><?php echo $aTeamData['sBusinessStream']?></td>
<td><?php echo $aTeamData['Pass']?></td>
<td><?php echo $aTeamData['Pass with Feedback']?></td>
<td><?php echo $aTeamData['Fail with Minors']?></td>
<td><?php echo $aTeamData['Fail with Majors']?></td>
</tr>
</tbody>
<?php foreach ($aQualityAgentResults AS $iUserId => $aAgentData) {
$aQualityPropertyResults = $oRadiusQualityFns->GetQualityPropertyResults($sDateFrom, $sDateTo, $sTaskType, $aAgentData['iBusinessStreamId'], $aAgentData['Agent']);
?>
<tbody>
<tr class="Agent<?php echo $iUserId; ?>">
<td><?php echo $oRadiusUser->Get_User_Name($aAgentData['Agent']);?></td>
<td><?php echo $aAgentData['Pass'];?></td>
<td><?php echo $aAgentData['Pass with Feedback'];?></td>
<td><?php echo $aAgentData['Fail with Minors'];?></td>
<td><?php echo $aAgentData['Fail with Majors'];?></td>
</tr>
</tbody>
<?php foreach ($aQualityPropertyResults AS $iUserId2 => $aPropertyData) { ?>
<tbody>
<tr class="Property<?php echo $iUserId2 ;?>">
<td colspan="2"><font color="black"><?php echo $aPropertyData['sPropertyCode']?></font></td>
<td colspan="3"><?php echo $aPropertyData['Result']?></td>
</tr>
</tbody>
<?php
}
}
}
?>
</table>
I'm putting the identifiers in classes, but if you want put in IDs you can, is the same thing, like this id="<?php ... ?>"

dompdf page break with codeigniter and foreach loop

I am using codeigniter and dompdf to generate a pdf document.
the document is generated with a foreach loop. each loop creates a few table rows. I want to have a pagebreak between the rows of each loop.
my relevent codeigniter view syntax is:
<table width=100%>
<?php foreach($summary as $summary){ ?>
<tr class="firstrow">
<td colspan="10">
<?php echo "<b>Customer</b>: <font color='blue'>".$summary->caccountname; ?>
<?php echo "</font> <b>Order Number</b>: <font color='blue'>".$summary->OrderNum; ?>
<?php echo "</font> <b>Sales rep</b>: <font color='blue'>".$summary->RepName; ?>
<?php echo "</font> <b>Due Date</b>: <font color='blue'>".substr($summary->DueDate,0,10); ?>
</td>
</tr>
<tr>
<td colspan="10"><hr>
</td>
</tr>
<tr>
<td colspan="10">
<table class="sortable" width="90%">
<tr><td ><b>Product</td><td ><b>Ordered</td><td ><b>Processed</td><td ><b>Outstanding</td><td ><b>Dispatched</td><td ><b>Available (incl FJ)</td><td ><b>Available FJ</td></tr>
<?php foreach($$linedetails as $linedetails){ ?>
<tr>
<td><?php echo $linedetails->Product; ?></td>
<td><?php echo $linedetails->cubicvolume; ?></td>
<td><?php echo $linedetails->Processed; ?></td>
<td><?php echo $linedetails->Outstanding; ?></td>
<td><?php echo $linedetails->Dispatched; ?></td>
<td><?php echo $linedetails->TotalVolume; ?></td>
<td><?php echo $linedetails->FJVolume; ?></td>
</tr>
<?php } ?>
</table>
</td>
</tr>
<?php } ?>
</table>
I have given the first tr a class of firstrow, I would like a page break to appear before this line every time.
As such I have applied the following style sheet in my page header:
<style>
#media print
{
.firstrow {page-break-before:always}
}
</style>
This doesnt work.
How can I acheive my desired result of placeing a page break before each loop firstrow TR?
Thanks as always
To make Pagebreak work with tr, write a line of css for tr
tr { page-break-inside:avoid; page-break-after:auto }
Try above code.
If not working, then add
table { page-break-inside:auto }
Try this, I am doing reports exactly same way
<?php
$nbsp5 = " ";
foreach($summary as $summary) { ?>
<table width="100%" style="page-break-after:always;">
<tr>
<td colspan="10">
<?php echo "<b>Customer</b>: <font color='blue'>".$summary->caccountname; ?>
<?php echo "</font>".$nbsp5 ." <b>Order Number</b>: <font color='blue'>".$summary->OrderNum; ?>
<?php echo "</font>".$nbsp5 ." <b>Sales rep</b>: <font color='blue'>".$summary->RepName; ?>
<?php echo "</font>".$nbsp5 ." <b>Due Date</b>: <font color='blue'>".substr($summary->DueDate,0,10); ?>
</td>
</tr>
<tr>
<td colspan="10"><hr></td>
</tr>
<tr>
<td colspan="10">
<table class="sortable" width="90%">
<tr>
<td><b>Product</b></td>
<td><b>Ordered</b></td>
<td><b>Processed</b></td>
<td><b>Outstanding</b></td>
<td><b>Dispatched</b></td>
<td><b>Available (incl FJ)</b></td>
<td><b>Available FJ</b></td>
</tr>
<?php foreach($$linedetails as $linedetails){ ?>
<tr>
<td><?php echo $linedetails->Product; ?></td>
<td><?php echo $linedetails->cubicvolume; ?></td>
<td><?php echo $linedetails->Processed; ?></td>
<td><?php echo $linedetails->Outstanding; ?></td>
<td><?php echo $linedetails->Dispatched; ?></td>
<td><?php echo $linedetails->TotalVolume; ?></td>
<td><?php echo $linedetails->FJVolume; ?></td>
</tr>
<?php } ?>
</table>
</td>
</tr>
</table>
<?php } ?>
(first <table> tag moved inside loop)
Please try this answer for both header and footer.

Categories