display check or x boolean in table - html

I have atable in mysql database i'm fetching it to a html
print "<table>\n";
$result = $con->query($query); //return only the first row (we only need field names)
$row = $result->fetch(PDO::FETCH_ASSOC);
print "<tr>\n";
foreach ($row as $field => $value){
print "<th>$field</th>\n";
} // end foreach
print "</tr>\n"; //second query gets the data
$data = $con->query($query);
$data->setFetchMode(PDO::FETCH_ASSOC);
foreach($data as $row){
print " <tr>\n";
foreach ($row as $name=>$value){
print "<td>$value</td>\n";
} // end field loop
print "</tr>\n"; } // end record loop
print "</table>\n";
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
} // end try
i have 2 columns that have a boolean value, i want that if column 6 = '0' then display x color:red; else display ✔ color:green;
Final Code
foreach ($row as $name=>$value){
if (($name == "paid" || $name == "added") && $value == "0"){
print "<td><span style='color: red;'>X</span></td>\n";
}
elseif (($name == "paid" || $name == "added") && $value == "1"){
print "<td><span style='color: lime;'>✔</span></td>\n";
}
else {
print "<td>$value</td>\n";
}
} // end field loop

Try:
foreach ($row as $name=>$value){
if ($name == "Column 6"){
if ($value == "0") {
print "<td><span style='color: red;">X</span></td>\n";
}
else {
print "<td>✔</td>\n";
}
}
else {
print "<td>$value</td>\n";
}
} // end field loop
There's no good way to colorize checkboxes, unless you want to use some complex css. However you could try and utilize before and after CSS for checkboxes as shown here: CSS ''background-color" attribute not working on checkbox inside <div>

Related

What's the best approach to display price ($$$) dynamically from table?

I have a table field that stores the price range. (Out of 5)
I want to display it in this manner.
If the range is 3, then display $$$ and other 2 ($) muted.
What is the best approach or what is this called?
(Check the below image for reference)
You could make use of for loop as follows:
$range = 3;
for($i=0; $i<5; $i++){
if($i < $range){
echo "<strong>$</strong>";
}else{
echo "<span class='text-muted'>$</span>";
}
}
and you have to add CSS like:
.text-muted {
color: #777;
}
Update
You can also use it as a function:
<?php
function displayRangeAsDollar($range){
$boldText = '';
$mutedText = '';
for($i=0; $i<5; $i++){
if($i < $range){
$boldText .= '$';
}else{
$mutedText .= '$';
}
}
return "<strong>".$boldText."</strong>"."<span class='text-muted'>".$mutedText."</span>";
}
echo displayRangeAsDollar(3);

JSON to HTML Table?

How can I generate html table form this url's json output?
http://megagrup.site/entegrasyon/hepsiburada.php
Update
I will find result like this;
<?php
$json=file_get_contents("url");
$data = json_decode($json);
if (count($data->listings)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->listings as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>".$stand->abc."</td>";
echo "<td>".$stand->def."</td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
I will find result like this;
<?php
$json=file_get_contents("url");
$data = json_decode($json);
if (count($data->listings)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->listings as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>".$stand->abc."</td>";
echo "<td>".$stand->def."</td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
?>
If I understand your question correctly you want to output the properties of the $stand object. You need an additional loop to iterate over the properties.
<?php
$json=file_get_contents("http://megagrup.site/entegrasyon/hepsiburada.php");
$data = json_decode($json);
if (count($data->listings)) {
// Open the table
echo "<table>\n";
// Cycle through the array
foreach ($data->listings as $idx => $stand) {
// Output a row
echo " <tr>\n";
// Output a cell for each property of the $stand object
foreach ($stand as $key => $value) {
echo " <td>" . $value . "</td>\n";
}
echo " </tr\n";
}
// Close the table
echo "</table>\n";
}
?>

What is wrong with my Perl script?

Probably everything in my opinion!
I basically don't know what I'm doing. I've never written a .cgi file before, but I did write the script basing myself on several tutorials...
One of them is this one...
http://www.yourhtmlsource.com/cgi/processingforms.html
I want a Perl script that will process an html form, collect the user's data and send to email with a return Thank you.html file to user when they hit SUBMIT.
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#pairs = split(/&/, $buffer);
foreach $pair (#pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
if( $FORM{magazine} ){
$magazine_flag ="ON";
}else{
$magazine_flag ="OFF";
}
if( $FORM{internet} ){
$internet_flag ="ON";
}else{
$internet_flag ="OFF";
}
if( $FORM{distributor} ){
$distributor_flag ="ON";
}else{
$distributor_flag ="OFF";
}
if( $FORM{yes1} ){
$yes1_flag ="ON";
}else{
$yes1_flag ="OFF";
}
if( $FORM{no1} ){
$no1_flag ="ON";
}else{
$no1_flag ="OFF";
}
if( $FORM{twofifty} ){
$twofifty_flag ="ON";
}else{
$twofifty_flag ="OFF";
}
if( $FORM{fivehundred} ){
$fivehundred_flag ="ON";
}else{
$fivehundred_flag ="OFF";
}
if( $FORM{thousand} ){
$thousand_flag ="ON";
}else{
$thousand_flag ="OFF";
}
if( $FORM{twofivehundred} ){
$twofivehundred_flag ="ON";
}else{
$twofivehundred_flag ="OFF";
}
if( $FORM{fivethousand} ){
$fivethousand_flag ="ON";
}else{
$fivethousand_flag ="OFF";
}
if( $FORM{tenthousand} ){
$tenthousand_flag ="ON";
}else{
$tenthousand_flag ="OFF";
}
if( $FORM{fifteenthousand} ){
$fifteenthousand_flag ="ON";
}else{
$fifteenthousand_flag ="OFF";
}
if( $FORM{yes2} ){
$yes2_flag ="ON";
}else{
$yes2_flag ="OFF";
}
if( $FORM{no2} ){
$no2_flag ="ON";
}else{
$no2_flag ="OFF";
}
if( $FORM{yes3} ){
$yes3_flag ="ON";
}else{
$yes3_flag ="OFF";
}
if( $FORM{no3} ){
$no3_flag ="ON";
}else{
$no3_flag ="OFF";
}
$Magazine-name = $FORM{magazine-name};
$Name = $FORM{name};
$Title = $FORM{title};
$Job = $FORM{job};
$Company = $FORM{company};
$Address = $FORM{address};
$City = $FORM{city};
$State = $FORM{state};
$Zip = $FORM{zip};
$Telephone = $FORM{telephone};
$E-mail = $FORM{e-mail};
open (MESSAGE,"| /usr/sbin/sendmail -t");
print MESSAGE "To: aalmeida\#aemc.com\n";
print MESSAGE "From: " . $Name . ", reader\n";
print MESSAGE "Reply-to: " . $E-mail . "(" . $Name . ")\n";
print MESSAGE "Subject: NECA 2013 Registration to win from $Name \n\n";
print MESSAGE "$Name wrote:\n\n";
print MESSAGE "Where do you look most often for test instrumentation?:\n\n";
print MESSAGE "$magazine_flag\n\n";
print MESSAGE "$magazine-name\n\n";
print MESSAGE "$internet_flag\n\n";
print MESSAGE "$distributor_flag\n\n";
print MESSAGE "$representative_flag\n\n";
print MESSAGE "I have a need for insulation testing:\n\n";
print MESSAGE "$yes1_flag\n\n";
print MESSAGE "$no1_flag\n\n";
print MESSAGE "What test voltage is important?:\n\n";
print MESSAGE "$twofifty_flag\n\n";
print MESSAGE "$fivehundred_flag\n\n";
print MESSAGE "$thousand_flag\n\n";
print MESSAGE "$twofivehundred_flag\n\n";
print MESSAGE "$fivethousand_flag\n\n";
print MESSAGE "$tenthousand_flag\n\n";
print MESSAGE "$fifteenthousand_flag\n\n";
print MESSAGE "Is the ability to store test results from the
instrument important?:\n\n";
print MESSAGE "$yes2_flag\n\n";
print MESSAGE "$no2_flag\n\n";
print MESSAGE "Do you plan to purchase within the next 6 months?:\n\n";
print MESSAGE "$yes3_flag\n\n";
print MESSAGE "$no3_flag\n\n";
print MESSAGE "Name: $FORM{name}\n\n";
print MESSAGE "Title: $FORM{title}\n\n";
print MESSAGE "Job Function: $FORM{job}\n\n";
print MESSAGE "Company: $FORM{company}\n\n";
print MESSAGE "Address: $FORM{address}\n\n";
print MESSAGE "City: $FORM{city}\n\n";
print MESSAGE "State: $FORM{state}\n\n";
print MESSAGE "Zip: $FORM{zip}\n\n";
print MESSAGE "Telephone: $FORM{telephone}\n\n";
print MESSAGE "E-mail: $FORM{e-mail}\n\n";
close (MESSAGE);
exit(0);
When I hit submit from the browser, I get this error:
Software error:
Can't modify subtraction (-) in scalar assignment at \boswinfs05\home\users\web\b465\whl.caadmin\www\HTML-email\SP_ToolKitPROMO_NECA2013\functions\formmailer.cgi line 117, near "};"
Missing right curly or square bracket at \boswinfs05\home\users\web\b465\whl.caadmin\www\HTML-email\SP_ToolKitPROMO_NECA2013\functions\formmailer.cgi line 180, at end of line
syntax error at \boswinfs05\home\users\web\b465\whl.caadmin\www\HTML-email\SP_ToolKitPROMO_NECA2013\functions\formmailer.cgi line 180, at EOF
Execution of \boswinfs05\home\users\web\b465\whl.caadmin\www\HTML-email\SP_ToolKitPROMO_NECA2013\functions\formmailer.cgi aborted due to compilation errors.
For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.
How can I make the script work?
Any insight would be much appreciated or links to more in depth tutorials would also be great.
Thanks!
You are using hyphens in those identifiers.
$Magazine-name = $FORM{magazine-name};
$E-mail = $FORM{e-mail};
Change those to underscores.
Enable syntax highlighting in your editor. That will help you catch such errors quickly even before compiling.
$E-mail and $Magazine-name are not valid variable names. Only letters, numbers, and the underscore characters can be used in a variable name(*). Try $E_mail or $Email.
(*) - without a little symbol table wizardry or other hacks that leave an impression of Perl as a write-only language
Tips:
Always use use strict; use warnings;!
Use the following instead of handling the CGI yourself:
use CGI qw( );
my $cgi = CGI->new();
my $form = $cgi->Vars();
# Use $form->{...} instead of $FORM{...}
if( $form->{magazine} ){
$magazine_flag ="ON";
}else{
$magazine_flag ="OFF";
}
if( $form->{internet} ){
...
could be written as
$form->{$_} = $form->{$_} ? "ON" : "OFF"
for qw(
magazine
internet
...
);

Spilting tables up at wrong place when using an if and while loop

I'm trying to show 2 tables, 1 for shows and 1 for movies but at the moment it shows 1 table per record
Here's the page http://starsqa.com/ana-lucasey-about
Here's the new code snippet:
if ($result['order'] == 1 OR $result['order'] == 2 OR $result['order'] == 6 OR $result['order'] == 4) {
$qry_stringr = "SELECT roles.starID, starName, roles.knownFor, roles.`character`, roles.`year`, most, type FROM roles INNER JOIN stars ON roles.starID = stars.starID WHERE roles.starID = {$result['starID']} ORDER BY roles.`year` DESC";
$prepr = $pdo_conn->prepare($qry_stringr);
$prepr->execute(array());
$showsArray = array();
$moviesArray = array();
while ($rowr = $prepr->fetch(PDO::FETCH_ASSOC)) {
if ($rowr['type'] == 0) {
$showsArray[] = $rowr;
}
else if($rowr['type'] == 1) {
$moviesArray[] = $rowr;
}
}
echo "<table border='1' bgColor='white'><tbody><tr bgColor='lightgrey' style='color:black;'><td><b>Show</b></td><td><b>Role</b></td><td><b>Year</b></td></tr>";
foreach($showsArray as $row) {
$most = $rowr['most'] == 1 ? ' color:#DE5635;' : '' OR $rowr['most'] == 0 ? ' color:black;' : '';
echo "<tr><td><font style='{$most}'>{$rowr['knownFor']} </font></td>
<td><font style='{$most}'>{$rowr['character']} </font></td>
<td><font style='{$most}'>{$rowr['year']}</font></td></tr>";
echo "</tbody>";
}
echo "</table><br><br>";
echo "<table border='1' bgColor='white'><tbody><tr bgColor='lightgrey' style='color:black;'><td><b>Movie</b></td><td><b>Role</b></td><td><b>Year</b></td></tr>";
foreach($moviesArray as $row) {
$most = $rowr['most'] == 1 ? ' color:#DE5635;' : '' OR $rowr['most'] == 0 ? ' color:black;' : '';
echo "<tr><td><font style='{$most}'>{$rowr['knownFor']} </font></td>
<td><font style='{$most}'>{$rowr['character']} </font></td>
<td><font style='{$most}'>{$rowr['year']}</font></td></tr>";
echo "</tbody>";
}
echo "</table><br><br>";
}
You're creating a table for each row. If you look in your while loop, you can see that for each iteration of your data, you're creating a new table. There are a number of ways to fix this, I would create two arrays, one for shows and one for movies. During your loop, add the row to the appropriate array. After all that, loop through each array to create your tables.
For example:
$showsArray = array();
$moviesArray = array();
while ($rowr = $prepr->fetch(PDO::FETCH_ASSOC)) {
if ($rowr['type'] == 0) {
$showsArray[] = $rowr;
}
elseif($rowr['type'] == 1) {
$moviesArray[] = $rowr;
}
}
echo "<table border='1' bgColor='white'><tbody><tr bgColor='lightgrey' style='color:black;'><td><b>Show</b></td><td><b>Role</b></td><td><b>Year</b></td></tr>";
foreach($showsArray as $row) {
//create the table row here.
}
echo "</table>";
echo "<table border='1' bgColor='white'><tbody><tr bgColor='lightgrey' style='color:black;'><td><b>Movie</b></td><td><b>Role</b></td><td><b>Year</b></td></tr>";
foreach($moviesArray as $row) {
//create the table row here.
}
echo "</table>";
Another way to do this is during the while loop, instead of using echo, append the HTML to a variable and then output it after the loop.
$movies = "";
$shows = "";
while ($rowr = $prepr->fetch(PDO::FETCH_ASSOC)) {
echo "{$rowr['type']} - {$rowr['knownFor']} ";
if ($rowr['type'] == 0) {
$shows .= "<tr><td><font>{$rowr['knownFor']} </font></td>
<td><font>{$rowr['character']} </font></td>
<td><font>{$rowr['year']}</font></td></tr>";
$shows .= "</tbody></table><br><br>";
}
if ($rowr['type'] == 1) {
$movies .= "<tr><td><font>{$rowr['knownFor']} </font></td>
<td><font>{$rowr['character']} </font></td>
<td><font>{$rowr['year']}</font></td></tr>";
$movies .= "</tbody></table>";
}
}
//add code here to create your
//tables and echo $shows and $movies in the appropriate places.

How can I paginate within a while loop?

I basically need to have take some videos information out of a database with a while loop and put them into a div. The only issue is that I need to put only 6 at a time in between a and tag and have it go to the next 6 and so forth. Here's my code:
$count = 0;
$sql = "SELECT * FROM videos ORDER BY id DESC";
$result_set = $database->query($sql);
while($videos = $database->fetch_array($result_set)) {
$count++;
// i know this is horribly wrong...
if($count == 0 || (($count % 6)+1 == 1)) {
echo '<div>';
}
// i need 6 videos to go in between the <div> and </div> tags then go on to another 6
echo "{$videos['title']}";
if($count == 0 || (($count % 6)+1 == 1)) {
echo '<div>';
}
}
This is an efficent way to do what you want:
$resultPerPage = 6;
$count = 0;
$sql = "SELECT * FROM videos ORDER BY id DESC";
$result_set = $database->query($sql);
$noPage = 1;
echo '<div id="page_1" class="pages">';
while($videos = $database->fetch_array($result_set)) {
$count++;
echo "{$videos['title']}";
if($count == $resultPerPage) {
echo '</div><div id="page_' . $noPage++ . '" class="pages">';
$count=0;
}
}
echo '</div>';