Displaying html text in loop only once php - php

I am having difficulty in this matter, i have like 2 columns with name of URL and Status and i have to post data into this html table in a loop, the problem that i am facing is the column title keeps looping itself again and again but i want it only once...this is my code
<?php
if (preg_match("/found/", $html))
{
echo '<table id="table-2">
<thead>
<tr>
<th>URL</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>'.$get_sites_link.'</td>
<td>Keyword Found</td>
</tr></tbody>
</table>';
$vul_url[] = $get_sites_link1;
$links_duplicate_removed = array_unique($vul_url);
file_put_contents($save_file, implode(PHP_EOL, $links_duplicate_removed));
}
else
{
echo '<table id="table-2">
<thead>
<tr>
<th>URL</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>'.$get_sites_link1.'</td>
<td>Keyword Not Found</td>
</tr></tbody>
</table>'."</b>"."</br>";
}
}
?>
Any help will be appreciated :)

Well, you should probably take the table opening and the header out of the loop. Something along the lines of:
echo "This is the only time this line will be shown.\n";
for ($i = 0; $i < 5; $i++) {
echo "This line was shown $i times!\n";
}
http://codepad.viper-7.com/Tis4wU

To show a thing many times, you need to iterate it, create a loop with:
for
foreach
while
do ... while
for($i=0; $i <=100; $i++){
echo "It's the $i time that I show it";
end

Related

For loop leaves blank td in a table

I am calling a for loop to get data from database with this code which is working fine but only with one problem
<table class="table" style="color:#000 !important;">
<tbody>
<tr>
<th>Question</th>
<th>User Answer</th>
</tr>
<tr>
</<?php
for ($x = 0; $x <= 30; $x++) {
?> <td><?= $info['ans_' . $x]; ?></td>
</tr>
<tr>
<?php
$sales_quiz = mysqli_query($conn_quiz, "SELECT * FROM `sales_quiz` where `que_no` = '$x'");
$sales_que = mysqli_fetch_array($sales_quiz); ?>
<td><?= $sales_que['que']; ?></td>
<?php }; ?>
</tr>
</tbody>
</table>
The result is first td of 2nd data which is que is blank and for loop start counting it from 2nd row
I do create a separate for loop for question and answer but then the table becomes broken.
If you need new rows in a table for every loop then you need to add the tr within the loop.
Also all tds that need to be on the same row has to be within one tr.
So remove the extra <tr> and bring them inside the loop.
<table class="table"
style="color:#000 !important;">
<tbody>
<tr>
<th>Question</th>
<th>User Answer</th>
</tr>
<?php
for ($x = 0; $x <= 30; $x++) {
?>
<tr>
<td><?= $info['ans_' . $x]; ?></td>
<!-- </tr> Remove these-->
<!-- <tr>-->
<?php
$sales_quiz = mysqli_query($conn_quiz, "SELECT * FROM `sales_quiz` where `que_no` = '$x'");
$sales_que = mysqli_fetch_array($sales_quiz);
?>
<td><?= $sales_que['que']; ?></td>
</tr>
<?php
} # the semi-colon (;) at the end is unnecessary
?>
</tbody>
</table>
You need to move your </tr><tr> to after the last td, and probably swap your $info['ans_'.$x]; and $sales_que['que']; since it seems like it should go question then answer, and the variable names make me think you are doing answer then question.
<table class="table" style="color:#000 !important;">
<tbody>
<tr>
<th>Question</th>
<th>User Answer</th>
</tr>
<tr>
<?php
for ($x = 0; $x <= 30; $x++) {
?> <td><?= $sales_que['que']; ?></td>
<?php
$sales_quiz = mysqli_query($conn_quiz, "SELECT * FROM `sales_quiz` where `que_no` = '$x'");
$sales_que = mysqli_fetch_array($sales_quiz); ?>
<td><?= $info['ans_'.$x]; ?></td>
</tr>
<tr>
<?php }; ?>
</tr>
</tbody>
</table>
I think it might be the "</" in the 10th line of your code, before the first <?php . There is any reason for that "</" to be there? From my perspective that creates a empty <tr></tr> on the resultant html file, and thats why you have that empty cell on your table. I might be wrong, just check it out and tell me if it works.

Loop in table row & display data which is available in array using PHP

I'm trying to loop in a table row 16 times with 2 elements of array. I know I can't loop 16 times with 2 elements of data but I want to display a table which has 16 row and only first 2 row will have data and others will leave blank.
Same as the image below
I have tried this code but its not what I want
<th>No</th>
<th>Id</th>
<th>Name</th>
for($i=1; $i<=16; $i++)
{
<tr>
<td> echo $i </td>
foreach($array as $a){
<td> $a->id </td>
<td> $a->name </td>
}
</tr>
}
You're looping through your entire array, 16 times. I assume that your array only has 2 elements in it. I'm going to assume it may not always have 2 elements in it, and if it has, say, 8 elements in it, you want the first 8 rows populated with data, but you always want 16 rows to display?
Additionally, I'm assuming that your $array variable is numerically indexed.
If all of that is true, then what you want is to eliminate your foreach, and just access the array element by index:
<thead>
<tr>
<th>No</th>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php for ($i=0; $i<=15; $i++) {
if (!empty($array[$i])) { ?>
<tr>
<td><?= $i+1; ?></td>
<td><?= $array[$i]->id; ?></td>
<td><?= $array[$i]->name; ?></td>
</tr>
<?php }
else { ?>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
Note that PHP arrays are zero-based, hence why I did 0-15 instead of 1-16.

How to display static values in td column in codeigniter

Hi all i have one table there i need to display the course report so every course has one stage so that i have 16 types of stages ex: On Hold,Asset Incomplete,SME Discussion..etc..Like 16 types i have So i would like to display 16 types in mt td column
<table class="table table-bordered">
<thead>
<tr><th class="center">#</th>
<th class="center">PHY</th>
<th class="center">CHE</th>
<th class="center">ZOO</th>
<th class="center">BOT</th>
<th class="center">MATH</th>
<th class="center">Total</th>
</TR>
</thead>
<tbody>
<tr>
<td>On Hold</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>
Remaining td's i have to get count of every course and to display
Can anyone help me how can i do that.
Thanks in advance.
You can do it by taking all your static string as an associative array for example
$arr = array(0=>'On Hold',1=>'Asset Incomplete',2=>'SME Discussion');
and your td should be
<?php $i = 0;
foreach($data as $dt){ ?>
<td><?php echo $arr[$i]; ?></td>
<?php $i++; } ?>
If you want to pass a variable from your controller to your view, you do so in the $data variable.
//Controller
$this->load->model('mymodel);
$data['var1'] = "Some value";
$data['var2'] = "Some value";
$data['query'] = $this->mymodel->thefunction();
//Model = mymodel
//function = thefunction(), you have to obviously create the class and function, if you don't know how to do that, let me know
$sql = "SELECT Count(courseid) as 'statusCount'";
$query = $this->db->query($sql);
When you now want to display those variables in your view template, call the variable by it's name, without the "$data part"
//In your view
<?php
foreach($query->result() as $row) {
echo "<td>" . $row->statusCount . "</td>";
}
?>
Please set the array with two fields.
One for Id,another for course name.
Then use forech() for write <td> in view.html.
For example:
forech($array as $item){
echo('<td><a href="ProjectView.php?course_id=".$item["id"]>$item["name"]'</a></td>') ;
}

Repeating Titles in headers when storing SQL data in table

When trying to display a table which has SQL data present, the rows at the top of the table are repeating, which I do not want to happen! I know it is probably something stupid but I've tried for a while to solve this and can't. Images outline the code which I used, and the output which is displayed
Write the first <tr></tr> before while
like this :
echo "<table>";
echo "<tr>
<th>albul Name</th>
..
..
..
</tr>";
while($album=$stmt->fetchObject()) {
echo "<tr>
<td>$album->$album_name
...
...
</tr>";
}
The issue is you're echoing your Header row inside of your WHILE loop, so you're writing the header for each iteration of the loop.
To fix, move the header row out of your loop like this:
echo "<table><tr>
<th>album Name</th>
<th>Year</th>
<th>Genre</th>
<th>Artist Name</th>
<th>Total Running Time</th>
</tr>"
while ($album = $stmt->fetchObject()) {
//Display the data as a row.
echo "<tr>
<td>$album->album_name</td>
<td>$album->year</td>
<td>$album->genre</td>
<td>$album->artist_name</td>
<td>$album->total_time</td>
</tr>"
}//end loop
echo "</table>";
When you're looking at something for too long you just can't see it anymore. Just take your tags out of the while loop. You only need to the data rows in the loop.
Take the code out of the WHILE loop.
The first row and should be before the loop code starts
you need change your code is firts set your struture html table and next php code:
<?php
echo"
<table border="1">
<tr>
<td>ALBUM</td>
<td>YEAR</td>
<td>GENERE</td>
<td>ARTIST</td>
<td>TOTAL PLAYING TIME</td>
</tr>
";
now you need print your records from db
while($album=$stmt->fetchObject()) {
echo "<tr>
<td>$album->$album_name</td>
<td>$album->$year</td>
<td>$album->$genre</td>
<td>$album->$artist_name</td>
<td>$album->$total_time</td>
</tr>";
}//end while
all code is this:
<?php
echo"
<table border="1">
<tr>
<td>ALBUM</td>
<td>YEAR</td>
<td>GENERE</td>
<td>ARTIST</td>
<td>TOTAL PLAYING TIME</td>
</tr>
";
while($album=$stmt->fetchObject()) {
echo "<tr>
<td>$album->$album_name</td>
<td>$album->$year</td>
<td>$album->$genre</td>
<td>$album->$artist_name</td>
<td>$album->$total_time</td>
</tr>";
}//end while

Is there anyway that I can get the table row with a specific td value with PHP or Simple HTML DOM?

Say I have the following table:
<table>
<thead>
<tr>
<th>Country<th>
<th>Currency Name</th>
<th>Currency Code</th>
<tbody>
<tr>
<td>Austria</td>
<td>Euro</td>
<td>EUR</td>
</tr>
<tr>
<td>South Africa</td>
<td>South African Rand</td>
<td>ZAR</td>
</tr>
<tr>
<td>Greece</td>
<td>Euro</td>
<td>EUR</td>
</tr>
</tbody>
</table>
How can I get the whole <tr> and contents containing <td>ZAR</td>? With Simple HTML DOM and PHP?
$string = '...'; // your HTML string
$html = str_get_html($string);
foreach ($html->find('tr') as $tr) {
$flag = 0;
foreach ($tr->find('td') as $td) {
if ($tr->plaintext === 'ZAR') {
$flag = 1;
break;
}
}
if ($flag === 1) {
echo $tr."\n";
}
}
Simple isn't smart enough to do it directly, but with most decent libraries (phpquery?) you can do:
$doc->find('tr:has(td:contains("ZAR"))')

Categories