Foreach and skip specific HTML - php

I am using a foreach to elaborate some values scraped from a web site. It works but I have the follow problem (I put screens to explain my problem in the best way).
In the first screen, you can see that Derry City - Limerick doesn't have 1 X 2 odds
But when I get that values, my code fill empty odds with the first odds available, see the screen below:
This is the HTML code:
<table class="table-main js-tablebanner-t js-tablebanner-ntb">
<tr><th class="h-text-left" colspan="2">2. Round</th><th> </th><th class="table-main__bs">B's</th><th class="h-text-center">1</th><th class="h-text-center">X</th><th class="h-text-center">2</th></tr>
<tr>
<td class="table-main__datetime">21.03.2017 20:45</td>
<td class="h-text-left"><span>Derry City</span> - <span>Limerick</span></td>
<td class="h-text-right">
<ul class="list-tags"><li class="list-tags__item"><i class="icon icon__stream"></i><div class="list-tags__window list-tags__window--streams"><ul class="list-tags__window__in">
<li>Live streams</li>
<li>bwin</li>
<li>Sportingbet</li>
</ul></div></li></ul>
</td>
<td class="table-main__bs"> </td>
<td class="table-matches__odds" data-oid=""> </td>
<td class="table-matches__odds" data-oid=""> </td>
<td class="table-matches__odds" data-oid=""> </td>
</tr>
<tr><th class="h-text-left" colspan="2">5. Round</th><th> </th><th class="table-main__bs">B's</th><th class="h-text-center">1</th><th class="h-text-center">X</th><th class="h-text-center">2</th></tr>
<tr>
<td class="table-main__datetime">18.03.2017 15:00</td>
<td class="h-text-left"><span>Limerick</span> - <span>Finn Harps</span></td>
<td class="table-matches__odds" data-oid="2kou5xv464x0x5uaff"></td>
<td class="table-matches__odds" data-oid="2kou5xv498x0x0"></td>
<td class="table-matches__odds" data-oid="2kou5xv464x0x5uafg"></td>
</tr>
<tr>
<td class="table-main__datetime">18.03.2017 16:00</td>
<td class="h-text-left"><span>Derry City</span> - <span>Drogheda</span></td>
<td class="table-main__bs">23</td>
<td class="table-matches__odds" data-oid="2kp2nxv464x0x5uaoj"></td>
<td class="table-matches__odds" data-oid="2kp2nxv498x0x0"></td>
<td class="table-matches__odds" data-oid="2kp2nxv464x0x5uaok"></td>
</tr>
and, at least, this is my code:
$match_dates = $html->find("td[class=table-main__datetime]");
$titles = $html->find("a[class=in-match]"); // 1 per match
$odds = $html->find("td[class=table-matches__odds]/a"); // 2
function print_odd($odd) {
if (array_key_exists('data-odd', $odd->attr)) {
return $odd->attr['data-odd'];
}
return $odd->children(0)->children(0)->children(0)->attr['data-odd'];
}
$c=0; $b=0; $o=0; $z=0; // two counters
foreach ($titles as $match) {
list($num1, $num2) = explode(':', $res->innertext); // <- explode
list($home, $away) = explode('-', $titles[$z++]->innertext); // <- explode
list($date, $time) = explode('-', $match_dates[$o++]->innertext); // <- explode
$odd1 = print_odd($odds[$b++]);
$odd2 = print_odd($odds[$b++]);
$odd3 = print_odd($odds[$b++]);
$home = strip_tags($home);
$away = strip_tags($away);
$uniquefield = $home . ' ' . $away;
echo "<tr><td class='rtitle'>".
//"<td class='last-cell'>".$match_dates[$o++]->innertext. "</td> " .
"<td> ".$date.'</td><td> : </td><td>'.$time . " / " .
"<td> ".$home.'</td><td> : </td><td>'.$away . " / " .
"<td class='odds'>".$odd1 . ";" .
"".$odd2 . ";" .
"".$odd3 . "</td>" .
"".$uniquefield."</td></tr><br/>";

Related

php script can't browse pages pagination not works maybe typo or variable

My PHP script can't browse pages and I can't locate this problem or where the error is.
So the problem is I can't browse pages... For example, I have 19 pages, and when I click on any number to go to that page, it doesn't browse to where it is supposed to, it just stays on page one.
The file causing that, is browse.php, I added the code below.
// The PREVIOUS page begins at the current offset LESS the number of ROWS per page
$previous_offset = $offset - ROWS;
// The NEXT page begins at the current offset PLUS the number of ROWS per page
$next_offset = $offset + ROWS;
// format query string depending on if
// mod_rewrite is on of off
if (REWRITE_URLS == 1) {
$browse_script_name = $browse_script_name . "?offset=";
} else {
$browse_script_name = $browse_script_name . "&offset=";
}
// show the row numbers that are being viewed like this:
// "10 - 15 of 20"
$browse_info = "
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td align=\"center\" valign=\"middle\" style=\"white-space: nowrap;
font-weight: bold;\">Showing " . ($offset + 1) . " - " . ($row_counter + $offset) . " of $rows_found Articles</td>
</tr>
</table>";
// Are there any PREVIOUS Pages?
if ($offset > 0)
{
// yes, so create a link
$browse_previous = "
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td style=\"white-space: nowrap;\"><a class=\"browse\" href=\"$browse_script_name" . $previous_offset . $browse_string . "\">« Previous</a></td>
</tr>
</table>";
} else {
// no there is no previous page so dont make a link
$browse_previous = "
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td style=\"white-space: nowrap;\">« Previous</td>
</tr>
</table>";
}// end if ($offset > 0)
// are there any NEXT pages
if ((ROWS != false) && ($rows_found > $next_offset))
{
// yes so create a link
$browse_next = "
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td style=\"white-space: nowrap;\"><a class=\"browse\" href=\"$browse_script_name" . $next_offset . $browse_string . "\">Next »</a></td>
</tr>
</table>";
} else {
// no so dont create a link
$browse_next = "
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td style=\"white-space: nowrap;\">Next »</td>
</tr>
</table>";
}// end if (($row != false) && ($rows_found > $next_offset))
// initialize $browse_page
$browse_page = "
<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">
<tr>
<td>";
// output page numbers as links
for ($x = 0, $page = 1; $x < $rows_found; $x += ROWS, $page++)
{
// is this the current page?
if ($x < $offset || $x > ($offset + ROWS - 1))
{
// no, so make a link
$browse_page .= " <a class=\"browse\" href=\"$browse_script_name" . $x . $browse_string . "\">". $page . "</a> ";
} else {
// yes, so dont make a link
$browse_page .= " $page ";
}// end if ($x < $offset || $x > ($offset + ROWS - 1))
}// end for ($x = 0, $page = 1; $x < $rows_found; $x += ROWS, $page++)
$browse_page .= "
</td>
</tr>
</table>";
Maybe this line is the problem?
"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td style=\"white-space: nowrap;\"><a class=\"browse\" href=\"$browse_script_name" . $previous_offset . $browse_string . "\">« Previous</a></td>
</tr>
</table>";
I noticed you do leave your '$browse_script_name' within your quotes, then you take write the other variables outside the quotes. And you do the same thing for all the lines. Perhaps you'd look into something like this:
<?php
// previous code ...
$browse_previous = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td style=\"white-space: nowrap;\"><a class=\"browse\" href=\"" . $browse_script_name . $previous_offset . $browse_string . "\">« Previous</a></td>
</tr>
</table>";
// remaining code ...
Now this would be a very superficial understanding of what is going on in the backend, I am only attemping to understand the logic, if this does not have try to provide a better background of the problem you're having.

Grading system using php and mysql

In the database, some courses value are NULL, and I don't want to print courses that hava a NULL value.
I don't know how I can achieve this in php or mysql. I need something like so:
if(course1!=NULL || course2!=NULL || course3!=NULL || course4!=NULL){
// echo course1; echo course2; echo course3; echo course4; }
The database looks like this:
And the output so far look like this:
A remark() function which check whether the course is A, B, C, D, E, or F is not working properly.
I want the remark() function to output either Excellent, Credit, Pass, or fail base on the grade.
Follows is the code:
function remark(){
global $mathematics, $physics, $chemistry, $lang;
if($mathematics=='A' || $physics=='A' || $chemistry=='A' || $lang=='A'){
$remark = "Excellent";
}
elseif($mathematics=='B' || $physics=='B' || $chemistry=='B' || $lang=='B' || $mathematics=='C' || $physics=='C' || $chemistry=='C' || $lang=='C'){
$remark = "Credit";
}
elseif($mathematics=='D' || $physics=='D' || $chemistry=='D' || $lang=='D' || $mathematics=='E' || $physics=='E' || $chemistry=='E' || $lang=='E'){
$remark = "Pass";
}
else{
$remark = "Fail";
}
return $remark;
}
if(isset($_POST['student_id']) && !empty($_POST['student_id'])){$student_id = $_POST['student_id'];
$check = "SELECT * FROM sample1 WHERE student_id='$student_id'";
$check_query = mysqli_query($connection, $check);
if(mysqli_num_rows($check_query)>0){
while($query_row = mysqli_fetch_assoc($check_query)){
$mathematics = $query_row['mathematics'];
$physics = $query_row['physics'];
$chemistry = $query_row['chm'];
$lang = $query_row['lang'];
echo "<table border=1 width=50%>
<tr> <td>Name: ".$query_row['name']." </td>
<td>Student Id: ".$query_row['student_id']."</td>
<td>Level: ".$query_row['level']."</td>
</tr>
<tr>
<th> Course </th>
<th> Grade </th>
<th> Remark </th>
</tr>
<tr> <td>Mathematics </td> <td>".$mathematics." </td></td> <td>".remark()." </td></tr>
<tr> <td>Physics </td> <td>".$physics."</td> </td><td>".remark()." </td></tr>
<tr> <td>Chemistry </td> <td>".$chemistry."</td> </td><td>".remark()." </td></tr>
<tr> <td>Language </td> <td>".$lang."</td> </td><td>".remark()." </td></tr>
</table>";
}
}
else{echo "Record not found";}
}
About the MYSQL not null values, you use this:
SELECT * FROM yourtable WHERE course1 IS NOT NULL
So, IS NOT NULL will give you queries where that column (course1) is not null. Here more info.
And for PHP, your remark() function has a wrong approach. I will give you one that is correct for your purpouse.
function remark($course){
switch($course) {
case 'A':
return 'Excellent';
break;
case 'B':
case 'C':
return 'Credit';
break;
case 'D':
case 'E':
return 'Pass';
break;
default:
return 'Fail';
break;
}
}
So in your HTML table you do this for each course:
while ($query_row = mysqli_fetch_assoc($check_query)) {
$mathematics = $query_row['mathematics'];
$physics = $query_row['physics'];
$chemistry = $query_row['chm'];
$lang = $query_row['lang'];
echo "<table border=1 width=50%>
<tr> <td>Name: " . $query_row['name'] . " </td>
<td>Student Id: " . $query_row['student_id'] . "</td>
<td>Level: " . $query_row['level'] . "</td>
</tr>
<tr>
<th> Course </th>
<th> Grade </th>
<th> Remark </th>
</tr>"
if(!is_null($mathematics)) echo "<tr> <td>Mathematics </td> <td>" . $mathematics . " </td></td> <td>" . remark($mathematics) . " </td></tr>";
if(!is_null($physics)) echo "<tr> <td>Physics </td> <td>" . $physics . "</td> </td><td>" . remark($physics) . " </td></tr>";
if(!is_null($chemistry)) echo "<tr> <td>Chemistry </td> <td>" . $chemistry . "</td> </td><td>" . remark($chemistry) . " </td></tr>";
if(!is_null($lang)) echo "<tr> <td>Language </td> <td>" . $lang . "</td> </td><td>" . remark($lang) . " </td></tr>";
echo "</table>";
}
Quick answer for the first issue extending the answer of #P0IT10n (?):
<?php
if(!is_null($mathematics)) {
?>
<tr> <td>Mathematics </td> <td>".$mathematics." </td></td> <td>".remark($mathematics)." </td></tr>
<?php
}
?>
// repeat for other courses
BUT
rethink your database structure.
Once you have another course you're f***...
Courses should have their own table, results another one.
Read about database normalisation!
EDIT
Another extension:
It would be even easier if you'd have an array like this:
$remarks = array('A'=>'Excellent', 'B'=>'Good',...);
then you need no extra function with a switch, all you need to do is:
echo $remarks[$mathematics]; // -> Excellent

How to automatically increment checkbox id in a table

I'm using this code to get all data from database and display it in a table:
class Product
{
private $conn;
private $id;
private $name;
private $description;
private $price;
private $category_id;
private $category_name;
private $created;
public function __construct($db)
{
$this->conn = $db;
}
public function readAll()
{
$stmt = $this->conn->prepare('SELECT name, description, price, CategoryID, created FROM products');
$stmt->execute();
echo "<table class=\"highlight responsive-table\">
<thead>
<tr>
<th data-field=\"empty\"> </th>
<th data-field=\"name\">Name</th>
<th data-field=\"description\">Description</th>
<th data-field=\"price\">Price</th>
<th data-field=\"category\">Category</th>
<th data-field=\"action\">Action</th>
</tr>
</thead>";
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
$n = $result['name'];
$d = $result['description'];
$p = $result['price'];
$ca = $result['CategoryID'];
$c = $result['created'];
echo "<tbody>
<tr>
<td style=\"width:10%;\">
<input type=\"checkbox\" id=\"checkbox\" />
<label for=\"checkbox\"></label>
</td>
<td style=\"width:15%;\">" .$n. "</td>
<td style=\"width:30%;\">" . $d. "</td>
<td style=\"width:10%;\">" ."$".$p. "</td>
<td style=\"width:15%;\">" . $ca. "</td>
<td style=\"width:20%;\">
<a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">mode_edit</i></a>
<a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">delete</i></a>
</td>";
}
echo "</tbody> </table>";
}
}
It's working, but in the 1st column I want to have a checkbox so I can mark records I want to delete / edit, but for now, whichever checkbox I click, only the 1st one get checked. How can I automatically increment checkbox id?
I would recommend using the id field from the products table. If it doesn't have one, I would add it.
Then in your html change the checkbox line to:
<input type=\"checkbox\" id=\"checkbox".$id."\" name=\"delete_product\" value=\"".$id".\" />
<label for=\"checkbox".$id."\"></label>
I added name and value in case you are submitting the form, you can get that data in the POST if you end up using a form.
use a variable $i, initialize it with 1 before while loop and
inside your loop,increment it
replace your checkbox code like
<input type=\"checkbox\" id=\"checkbox_$i\" />
<label for=\"checkbox_$i\"></label>
$i++;
Try using a counting variable like this:
$i = 0;
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
$n = $result['name'];
$d = $result['description'];
$p = $result['price'];
$ca = $result['CategoryID'];
$c = $result['created'];
echo "<tbody>
<tr>
<td style=\"width:10%;\">
<input type=\"checkbox\" id=\"checkbox".$i."\" />
<label for=\"checkbox".$i."\"></label>
</td>
<td style=\"width:15%;\">" .$n. "</td>
<td style=\"width:30%;\">" . $d. "</td>
<td style=\"width:10%;\">" ."$".$p. "</td>
<td style=\"width:15%;\">" . $ca. "</td>
<td style=\"width:20%;\">
<a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">mode_edit</i></a>
<a class=\"waves-effect waves-light btn\"><i class=\"material-icons\">delete</i></a>
</td>";
$i++;
}
Add id column to your sql query. Then modify your first column in table to
<input type=\"checkbox\" id=\"checkbox\" . $result['id'] . " name=\"id[" . $result['id'] . "]\"/>
<label for=\"checkbox" . $result['id'] . "\"></label>
In Controller you will have access to selected ids using for example $_POST['id']

How do I pass values with a link to a new page?

What I want is to be able to click on the link (review.php) and then to display the review corresponding to that film only, on a new page.
Not sure if my MySQL query is wrong or if my A href link is not formatted correctly.
Any help would be appreciated.
require_once('./includes/mysql_connect.php');
$query = "SELECT films.movie_title, films.rating, films.actor, reviewed.review
FROM films
INNER JOIN reviewed
ON films.movie_id=reviewed.review_id";
$result = mysql_query($query) or die ("Could not execute mysql" . mysql_error()); // Run Query
$num = mysql_numrows($result);
if ($num > 0) { // If it ran ok, display records.
echo "<p> There are curently $num records.</p>";
// Table header.
echo '<table border="1" align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="left"><b>Movie Title</b></td>
<td align="left"><b>Leading Actor</b></td>
<td align="left"><b>Rating</b></td>
<td align="left"><b>Review</b></td>
</tr>';
// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td align="left">' . $row['movie_title'] . '</td>
<td align="left">' . $row['actor'] . '</td>
<td align="left">' . $row['rating'] . '</td>
<td align="left"><a href="review.php?id='. $row['review'] . '> Read Review </a>
</tr> ';
}
}
?>

how to give color to table tr?

Hello i have a table with some fields like
here i want make colors for table entire rows..means if ASR value is 75 to 100 should get one color and 50 to 75 should get another color and below 50 should get another color.
and here is my php code
<table width="75%" border="1">
<tr>
<td align="center">channel no</td>
<td align="center">IP</td>
<td align="center">Total calls</td>
<td align="center">Connected calls</td>
<td align="center">Disconnected calls</td>
<td align="center">Duration</td>
<td align="center">ASR</td>
<td align="center">ACD</td>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr>
<td>".$row['channel']." </td>
<td>".$row['ip']." </td>
<td>".$row['totalcalls']." </td>";
if ($row['totalcalls']>1){
$sql1 = "SELECT count(duration) as count FROM gateways where duration=0 and ip='".$_POST['ip']."' and channel='".$row['channel']. "' and (connect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' or disconnect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' ) Group by channel";
$result1 = mysql_query($sql1, $link);
$norow=mysql_fetch_assoc($result1);
$attenedcalls=($row['totalcalls']-$norow['count']);
echo "<td>".$attenedcalls." </td>";
$disconnectedcalls=($row['totalcalls']-$attenedcalls);
echo "<td>".$disconnectedcalls." </td>";
echo " <td>".$row['tduration']." </td>";
echo "<td>".(($attenedcalls/$row['totalcalls'])*100)."</td>";
}else{
echo "<td>".$row['totalcalls']."</td>";
echo "<td>100</td>";
}
$minutes = gmdate("H:i:s", ($row['tduration']/$attenedcalls));
echo " <td>".$minutes." </td>
</tr>";
}
?>
</table>
thanks in advance
You can try like this
<table width="75%" border="1">
<tr>
<td align="center">channel no</td>
<td align="center">IP</td>
<td align="center">Total calls</td>
<td align="center">Connected calls</td>
<td align="center">Disconnected calls</td>
<td align="center">Duration</td>
<td align="center">ASR</td>
<td align="center">ACD</td>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
$color = '';
if ($row['totalcalls']>1){
$sql1 = "SELECT count(duration) as count FROM gateways where duration=0 and ip='".$_POST['ip']."' and channel='".$row['channel']. "' and (connect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' or disconnect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' ) Group by channel";
$result1 = mysql_query($sql1, $link);
$norow=mysql_fetch_assoc($result1);
$attenedcalls=($row['totalcalls']-$norow['count']);
$asr = (($attenedcalls/$row['totalcalls'])*100);
if($asr >= 75 && $asr <=100 ){
$color = 'red';
}else if($asr >= 50 && $asr < 75){
$color = 'cyan';
}else if($asr < 50){
$color = 'blue';
}
}
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr style='background-color : ".$color."'>
<td>".$row['channel']." </td>
<td>".$row['ip']." </td>
<td>".$row['totalcalls']." </td>";
if ($row['totalcalls']>1){
echo "<td>".$attenedcalls." </td>";
$disconnectedcalls=($row['totalcalls']-$attenedcalls);
echo "<td>".$disconnectedcalls." </td>";
echo " <td>".$row['tduration']." </td>";
echo "<td>".$asr."</td>";
}else{
echo "<td>".$row['totalcalls']."</td>";
echo "<td>100</td>";
}
$minutes = gmdate("H:i:s", ($row['tduration']/$attenedcalls));
echo " <td>".$minutes." </td>
</tr>";
}
?>
</table>
[...]
while ($row = mysql_fetch_assoc($result)) {
$asrVal=(($attenedcalls/$row['totalcalls'])*100);
if($asrVal>=50 && $asrVal <=75) $class="from50to75";
if($asrVal>=75 && $asrVal <=100) $class="from75to100";
if($asrVal<50) $class="below50";
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr class='$class'>
[...]
then add:
<style>
tr.from50to75 td{background-color:red;}
tr.from75to100 td{background-color:green;}
tr.below50 td{background-color:blue;}
</style>
Modify your while loop so that you compute the ASR value before emitting the <tr> tag. Use that value to select a class according to the classification you have set up, and emit a tag of the form <tr class=foo> where foo is the class name you have selected. Then it’s just a matter of writing CSS rules for the classes, using class selectors like tr.foo.
(Provided that you have not set color on the td cells. If you have, you need to use selectors like tr.foo td to override such settings.)

Categories