I need to do a if statement inside a print, i have this
while($data=$results->fetchrow()) {
$id = $data['id'];
$stuff = $data['stuff'];
print ('
<tr>
<td>'.$id.'</td>'
if ($stuff == 1){
print "<td>".$age."<td><td> </td> ";
}else{
print "<td> </td><td> ".$age."</td>";
}'
<td>bla bla</td>
<td>bla bla</td>
</tr>
');
and this is not working for me, so please guys, help me! :)
Try this, I just moved it outside the print statement.
<?php
while($data=$results->fetchrow()) {
$id = $data['id'];
$stuff = $data['stuff'];
if ($stuff == 1){
$foo = "<td>".$age."<td><td> </td> ";
}else{
$foo = "<td> </td><td> ".$age."</td>";
}
print ('
<tr>
<td>'.$id.'</td>'
.$foo.
'<td>bla bla</td>
<td>bla bla</td>
');
You can use a conditional, aka ternary, expression:
print ('
<tr>
<td>'.$id.'</td>'.
($stuff == 1 ?
"<td>".$age."<td><td> </td> " :
"<td> </td><td> ".$age."</td>") .
'
<td>bla bla</td>
<td>bla bla</td>
');
Related
I am having a problem to convert my $quantity_total which is as example (113) from 3 different products.
I want it to be in a table like below.
I have been trying to use chunk_split and explode but if i was able to succeed in that. I wouldn't be able to make it dynamic.
<table>
<tr>
<th>Quantity</th>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>3</td>
</tr>
</table>
$total=0;
$item_count=0;
$arr = array();
$quantity_all = '';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$arr[] = $row;
$_SESSION['cart-checkout'] = $arr;
$quantity=$_SESSION['cart'][$id]['quantity'];
$quantity_all .=$quantity;
$sub_total=$price*$quantity;
echo "<div class='cart-row'>";
echo "<div class='col-md-8'>";
echo "<div class='product-name m-b-10px'><h4>{$name}</h4></div>";
echo $quantity>1 ? "<div>{$quantity} items</div>" : "<div>{$quantity} item</div>";
echo "</div>";
echo "<div class='col-md-4'>";
echo "<h4>$" . number_format($price, 2, '.', ',') . "</h4>";
echo "</div>";
echo "</div>";
$item_count += $quantity;
$total+=$sub_total;
$_SESSION['total'] = $total;
$_SESSION['item-count'] = $item_count;
}
$_SESSION['quantity-all'] = $quantity_all;
Is this possible? And i need it to be dynamic. So if it were 10 different quantities. It would make 10 table rows.
I hope someone can help me, would really appreciate it a lot! It's the last thing to finish my e-commerce webshop.
As mentioned in my comment, I don't think this is good solution... but the function you're looking for is str_split. https://stackoverflow.com/a/9814389/296555
http://sandbox.onlinephpfunctions.com/code/0bbee53cafafc0d5e8954e07d0abc2c86c6c89a8
<?php
$rows = '156165165489465131';
echo '<table>';
echo '<tr><th>Quantity</th></tr>';
foreach (str_split($rows) as $row) {
echo "<tr><td>$row</td></tr>";
}
echo '</table>';
I dont know if this Is what you want, but you can try something like:
<table>
<tr>
<td>Quantity</td>
</tr>
<?php
$characters = str_split( (string) $quantity_total);
foreach($characters as $char){
echo "
<tr>
<td> $char </td>
</tr>
";
}
?>
</table>
I have a table like this.
but I want the data on the 'debit' column is on it own place.
this is what Iwant :
this is my current code :
<?php
foreach ($jurnal->result_array() as $data){
echo "
<tr>
<td>".$data['no_akun']."</td>
<td>".$data['nm_akun']."</td>";
if ($data['nm_akun'] == 'kredit'){
echo "
<td></td>
<td>".$data['posisi_dr_cr']."</td>
";
}else{
echo "
<td>".$data['posisi_dr_cr']."</td>
<td></td>
";
}
"</tr>
";
}
?>
td tag width is the same of the content that it envelopes, so an empty td will be 0px width.
Try adding width="25%" to your td tags, like this:
EDITED:
foreach ($jurnal->result_array() as $data){
echo "
<tr>
<td width=\"25%\">".$data['no_akun']."</td>
<td width=\"25%\">".$data['nm_akun']."</td>";
if ($data['nm_akun'] == 'kredit'){
echo "
<td width=\"25%\"></td>
<td width=\"25%\">".$data['posisi_dr_cr']."</td>
";
}else{
echo "
<td width=\"25%\">".$data['posisi_dr_cr']."</td>
<td width=\"25%\"></td>
";
}
"</tr>
";
}
?>
Notice that width="25%" is typed inside a String, so double quote characters must have \ characters in order to be escaped.
Try this way. if you are using bootstrap css, you can use text-left in the class.
<?php
foreach ($jurnal->result_array() as $data){
echo "
<tr>
<td class='text-left'>".$data['no_akun']."</td>
<td class='text-left'>".$data['nm_akun']."</td>";
if ($data['nm_akun'] == 'kredit'){
echo "
<td class='text-left'></td>
<td class='text-left'>".$data['posisi_dr_cr']."</td>
";
}else{
echo "
<td class='text-left'>".$data['posisi_dr_cr']."</td>
<td class='text-left'></td>
";
}
"</tr>
";
}
?>
use the following code:
<?php
foreach ($jurnal->result_array() as $data){
echo "
<tr>
<td>".$data['no_akun']."</td>
<td>".$data['nm_akun']."</td>";
if ($data['nm_akun'] == 'kredit'){
echo "
<td> </td>
<td>".$data['posisi_dr_cr']."</td>
";
}else{
echo "
<td>".$data['posisi_dr_cr']."</td>
<td> </td>
";
}
"</tr>
";
}
?>
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
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.)
I have code which retrieves information about players from a MySQL database. I want to apply a special case to the HTML output if their ranking changes. I want it to look like this: http://i27.tinypic.com/f406tz.png
But i cant get it to be like i want, instead it prints the rank on every row:
$old_rank = '';
while ($g = mysql_fetch_object($q)) {
if ($g->rankname != $old_rank) {
echo "<tr><td>$g->rankname</td>\n";
$old_rank = "<tr><td> </td>\n";
}
echo " <td>$g->name</td></tr>\n";
}
What I want:
<tr>
<td>One</td>
<td>Kraven the Hunter</td>
</tr>
<tr>
<td> </td>
<td>Kull the Conqueror</td>
</tr>
<tr>
<td> </td>
<td>Zazi The Beast</td>
</tr>
<tr>
<td>Vice-leader</td>
<td>Igos du Ikana</td>
</tr>
<tr>
<td> </td>
<td>Saint Sinner</td>
</tr>
<tr>
<td> </td>
<td>Midvalley the Hornfreak</td>
</tr>.......................
What I get:
<tr><td>One</td>
<td>Tester</td></tr>
<tr><td>One</td>
<td>Kraven the Hunter</td></tr>
<tr><td>One</td>
<td>Kull the Conqueror</td></tr>
<tr><td>One</td>
<td>Zazi The Beast</td></tr>
<tr><td>Vice-Leader</td>
<td>Midvalley the Hornfreak</td></tr>
<tr><td>Vice-Leader</td>
<td>Saint Sinner
</td></tr>
<tr><td>Vice-Leader</td>
<td>Igos du Ikana</td></tr>
$old_rank is never equal to $g->rankname because the way you are setting $old_rank, it will contain HTML tags, and the $g->rankname that you get from the DB will never have HTML tags.
Try changing your if statement to something like this:
if ($g->rankname != $old_rank) {
echo "<tr><td>$g->rankname</td>\n";
$old_rank = $g->rankname;
} else {
echo "<tr><td> </td>\n";
}
It prints the rank name if it's a new rank name, else it prints empty space.
The following (notwithstanding typos) separates out the display logic from the database loop. This has the advantages:
- You don't need to depend on the order of the results returned
- You don't need to maintain dodgy logic (like 'old_rank')
- You can display them more nicely (with a rowspan for repeated ranks
I believe the total code is more compact too.
// fill ranks array
$ranks = array();
while ( $g = mysql_fetch_object($q) ) {
if ( !in_array($g->rankname, $ranks) ) {
$ranks[htmlentities($g->rankname)] = array();
}
$ranks[$g->rankname][] = htmlentities($g->name);
}
// do other program logic here
// end of program
?>
<!-- display the page -->
<table>
<tr>
<th>Rank</th><th>Users</th>
</tr>
<?php foreach($ranks as $rankName => $userList): ?>
<tr>
<td rowspan="<?php echo (string)sizeof($userList); ?>">
<?php echo $rankName; ?>
</td>
<td> <?php echo implode('</td></tr><tr><td>', $userList); ?> </td>
</tr>
<?php endforeach; ?>
</table>
I prefer breaking things up a bit more than that. Keeping things separate makes it easier to modify. This should work.
$old_rank = '';
while ($g = mysql_fetch_object($q)) {
echo '<tr>' . "\n";
echo '<td>';
if ($g->rankname != $old_rank) {
$old_rank = $g->rankname;
echo $old_rank;
} else {
echo ' ';
}
echo '</td>';
echo '<td>' . $g->name . '</td>' . "\n";
echo '</tr>' . "\n";
}