If Statement (PHP) - php

I need If Statement (php) to :
if (Modem = ModemNotReceived) and (CheckModem = Done)
Total + 50
And
if (Modem = ModemReceived) and (CheckModem = Done)
Total - 50
How i can do that in one if statement?
My Table :
| ID | Modem | CheckModem | Total
------------------------------------------------
| 1 | ModemReceived | Done | 120
------------------------------------------------
| 2 | ModemNotReceived | Null | 90
------------------------------------------------
| 3 | ModemReceived | Null | 100

If I understand what you're saying correctly, this code is what you mean:
<?php
if ($Modem == 'ModemNotReceived' && $CheckModem == 'Done') {
$Total += 50;
} else if ($Modem == 'ModemReceived') && $CheckModem == 'Done' {
$Total -= 50;
}
?>

Related

grouping item and count in data using loop

I have a data when i do
var_dump ($data);
here is how my data is displayed ($data is not a table balise):
+-------+----------+-----------+---------------+
| adan | Afla | avo | 0 |
+-------+----------+-----------+---------------+
| adan | ken | joly | 0 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 0 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 0 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 0 |
+-------+----------+-----------+---------------+
however, I would like the table display to become :
+-------+----------+-----------+---------------+
| adan | Afla | avo | 1 |
+-------+----------+-----------+---------------+
| adan | ken | joly | 1 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 3 |
+-------+----------+-----------+---------------+
here is my code :
foreach($data as $liste){
$town[]=$liste->getLocalite()->getName();
$counties[]=$liste->getCountie()->getName();
$districts[]=$liste->getDistrict()->getName();;
}
$a= count($listforages)-1;
$numberDoc=0;
$tab1=$tab2=$tab3=$tab4=array();
for ($i=0; $i<count($data);$i++){
$tab[$i]= $town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'.$numberDoc[$i];
if( $i == $a ) {
if ($counties [$a-1]==$counties [$a] && $districts[$a-1]==$districts[$a] && $numberDoc[$a-1]==$numberDoc[$a] ){
$numberDoc[$a]++; // counter
$tab1[$a-1]=$town[$a-1].'--'.$counties [$a-1].'--'.$districts[$a-1].'--'. $numberDoc[$a-1];
}
if ($counties [$a-1] !=$counties [$a] && $districts[$a-1] !=$districts[$a] && $numberDoc[$a-1] !=$numberDoc[$a] ){
$numberDoc[$a]++; // counter
$tab2[$a-1]=$town[$a-1].'--'.$counties [$a-1].'--'.$districts[$a-1].'--'. $numberDoc[$a-1];
}
}
else{
if ($counties [$i]==$counties [$i+1] && $districts[$i]==$districts[$i+1] && $numberDoc[$i]==$numberDoc[$i+1] ){
$numberDoc[$i]++; // counter
$tab3[$i]=$town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'. $numberDoc[$i];
}
if ($counties [$i] !=$counties [$i+1] && $districts[$i] !=$districts[$i+1] && $numberDoc[$i] !=$numberDoc[$i+1] ){
$numberDoc[$i]++; // counter
$tab4[$i]=$town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'. $numberDoc[$i];
}
}
}
when you see my data (see table) :
town is column 0,
counties is column 1,
district is column 2,
numberDoc is column 3 ,
my code don't work ,How resolve this issue ?
NB: excuse my english,
thank you in advance

Switch Case Looping PHP

i was very frustated, i've tried many method but didn't find my expexted result. I have this data in my table database:
| PartID | HasilProduksi |QtyProduksi|
|----------------------|------------------|-----------|
| BLAP-FG152-PF-KGX | Repair | 1 |
| AWDX-FG002-HN-KGX | Reject | 90 |
| HMXX-FG022-EG-KGX | Good | 100 |
| ECJX-FG018-AV-MCM | Good | 111 |
and i want to create report with result :
| | ACTUAL |
| PartID | ---------------------|
| |Good | Repair | Reject|
|----------------------|-----|--------|-------|
| BLAP-FG152-PF-KGX | 0 | 1 | 0 |
| AWDX-FG002-HN-KGX | 0 | 0 | 90 |
| HMXX-FG022-EG-KGX | 100 | 0 | 0 |
| ECJX-FG018-AV-MCM | 111 | 0 | 0 |
and i'm using switch case method but the result is not like with my expected result. And This is my code :
$sqlc = "SELECT * FROM $db_dthp WHERE IdBukti='$id_bukti'";
$qc = mysqli_query($conn, $sqlc);
$c = 0; $good=0; $reject=0; $repair=0;
while ($rc = mysqli_fetch_assoc($qc)) {
switch ($rc['HasilProduksi']) {
case 'Good':
$good += (int) $rc['QtyProduksi'];
$datagood += (int) $rc['QtyProduksi'];
break;
case 'Reject':
$reject += (int) $rc['QtyProduksi'];
$datareject += (int) $rc['QtyProduksi'];
break;
case 'Repair':
$repair += (int) $rc['QtyProduksi'];
$datarepair += (int) $rc['QtyProduksi'];
break;
default:
break;
}
$c++;
}
$res['result'][$i]['data'][$b]['hp'][0]['good'] = number_format($good,0,'.','.');
$res['result'][$i]['data'][$b]['hp'][0]['reject'] = number_format($reject,0,'.','.');
$res['result'][$i]['data'][$b]['hp'][0]['repair'] = number_format($repair,0,'.','.');
Can you help me?
If you use this other query, you will have already your data and you don't need the switch in php. You will do the case in your query:
$sqlc="SELECT PartID,
CASE
WHEN HasilProduksi='Good' THEN QtyProduksi ELSE 0
END as Good,
CASE
WHEN HasilProduksi='Repair' THEN QtyProduksi ELSE 0
END as Repair,
CASE
WHEN HasilProduksi='Reject' THEN QtyProduksi ELSE 0
END as Reject
FROM $db_dthp WHERE IdBukti='$id_bukti';"
You can use mysql if to do this
SELECT PartID,IF(HasilProduksi ='Good',QtyProduksi,0)as
GOOD,IF(HasilProduksi ='Repair',QtyProduksi,0)as REPAIR,
IF(HasilProduksi ='Reject',QtyProduksi,0)as REJECT FROM
$db_dthp group by PartID

Limit the number of Keywords in PHP

I need help limiting the number of keywords on my page. I can call the keywords from the database ex. $keywords is available for them. I want to get them for every blog post.
My database table is formatted like this
| id | title | ... | keywords |
|---------------------------------- |
| 1 | title1 | ... | word-t1-1 |
| | | | word-t1-2 |
| | | | word-t1-3 |
| | | | |
| 2 | title2 | ... | word-t2-1 |
| 3 | title3 | ... | word-t3-1 |
| 4 | title4 | ... | word-t4-1 |
I have this code:
<?php
$kw1 = trim($keywords);
$ct1 = explode("\n", $kw1);
$searchkeys = "";
for ($i = 0; $i <= sizeof($ct1); $i++) {
$searchkeys = $searchkeys.''.$ct1[$i].', ';
}
//echo $searchkeys;
?>
The code inside for loop shows the keywords as hyperlink
I want to know how to limit the number of printed keywords while if I define it in the for loop ex.
for ( $i = 0; i < 25; $i++){ somecode; }
Sometimes there aren't 25 keywords,if run the code it prints
keyword1, keyword2,,,,,,,,,,,,,,
My suggestion would be use the min() function like so:
for ( $i = 0; $i < min(count($arr), 25); $i++ ) { // code here }

Not getting expected results through foreach loop

I have following table structure
Updates
+----------------------------------------------------------------------------+
| Id | name | category | link | artist | home |
+---------------------------------------------------------------------------+
| 1 | song_1 | single | #### | artist_1 | 1 |
+-------+------------+----------------+-------------+--------------+
| 2 | song_2 | bollywood | #### | artist_2 | 1 |
+-------+------------+----------------+-------------+--------------+
| 3 | song_3 | single | #### | artist_3 | 1 |
+-------+------------+----------------+-------------+--------------+
| 4 | song_4 | bollywood | #### | artist_4 | 1 |
+------------------------------------------------------------------+
My code for fetch results.
<?php $update_list = $db->query('select * from `update` where home = 1 order by id desc LIMIT 0, 10');
foreach($update_list as $field => $value)
{
print_r($value['category']' - ');
if($value['link'] != '') { print_r(''.$value['name'].' - - ');
}
if($value['singers'] != '') {
print_r('['.$value['singers'].']');
} ?>
I'm getting following results
single - song_1- [artist_1]
bollywood - song_2 - [artist_2]
single - song_3 - [artist_3]
bollywood - song_4 - [artist_4]
I want results like this, by above single query.
single - song_1 - [artist_1] > song_3 - [artist_3]
bollywood - song_2 - [artist_2] > song_4 - [artist_4]
Can't figure out how to code it.
Help me please, suggest me a way which php function to use here.
Try the below code if it helps,
<?php
$update_list = $db->query('select * from `update` where home = 1 order by category desc, id desc LIMIT 0, 10');
$currentcategory = false;
foreach($update_list as $field => $value) {
if($currentcategory != $value['category']) {
if($currentcategory != false) {
echo '<br>';
}
echo $value['category'].' - ';
$currentcategory = $value['category'];
}
else {
echo ' > ';
}
if($value['link'] != '') {
echo ''.$value['name'].' - ';
}
if($value['singers'] != '') {
echo '['.$value['singers'].']';
}
} ?>

Coordinates from MySQL to HTML table (PHP, PDO, MySQL)

I am creating game which uses MySQL database to create "playing field".
In my MySQL table I have two columns pointX and pointY, both INT. I could also use POINT, but in my case these two columns are better solution.
| id | pointX | pointY | player | game |
|----|--------|--------|--------|------|
| 1 | -2 | 1 | 7 | 10 |
| 2 | -3 | 2 | 5 | 10 |
| 3 | 2 | -2 | 2 | 10 |
| 4 | -2 | -1 | 1 | 10 |
I should produce HTML table from this MySQL table. Something like this, but with no coordinateheaders (below those are only for easier understanding):
|-----|----|----|----|----|----|----|
| Y/X | -3 | -2 | -1 | 0 | 1 | 2 |
|-----|----|----|----|----|----|----|
| -2 | | | | | | 2 |
|-----|----|----|----|----|----|----|
| -1 | | 1 | | | | |
|-----|----|----|----|----|----|----|
| 0 | | | | | | |
|-----|----|----|----|----|----|----|
| 1 | | 7 | | | | |
|-----|----|----|----|----|----|----|
| 2 | 5 | | | | | |
|-----|----|----|----|----|----|----|
Plus.. every <td> should have attribute data-cell, which includes coordinates, as example data-cell="-2x-1".
What is the best way to get started?
$rng = $dbh->prepare('
SELECT MIN(pointX) AS minX, MIN(pointY) AS minY,
MAX(pointX) AS maxX, MAX(pointY) AS maxY
FROM field
WHERE game = ?
LOCK IN SHARE MODE
');
$qry = $dbh->prepare('
SELECT pointX, pointY, player
FROM field
WHERE game = ?
ORDER BY pointX, pointY
');
$dbh->beginTransaction();
$rng->execute([$game_id]);
$qry->execute([$game_id]);
$dbh->rollBack();
$limits = $rng->fetch();
$player = $qry->fetch();
echo '<table>';
for ($y = $limits['minY']; $y <= $limits['maxY']; $y++) {
echo '<tr>';
for ($x = $limits['minX']; $x <= $limits['maxX']; $x++) {
printf('<td data-cell="%dx%+dy">', $x, $y);
if ($player and $player['pointX'] == $x and $player['pointY'] == $y) {
echo htmlentities($player['player']);
$player = $qry->fetch();
} else {
echo ' ';
}
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
It looks like you are already off to a good start. Assuming the number of cells for X and Y as $xcells and $ycells, the next step I would take is to build your table like so:
echo "<table>";
for ($i = -3; $i < $ycells; i++) {
echo "<tr>";
for ($j = -3; $j < $xcells; j++) {
echo "<td data-cell='" . $i . "y" . $j . "x'>";
}
echo "</tr>";
}
echo "</table>";

Categories