I'm trying to have a "1" printed if there is a value in cDeviceRegistrationId column in the database. Here is the code:
$result is an SQL query
while($row = mysql_fetch_assoc($result))
{
if ($row['cDeviceRegistrationId'] > 0) {
$a = 1;
}
echo "<tr class='forum'>";
echo "<td class='forum'>" . $row['intUserID'] . "</td>";
echo "<td class='forum'>" . $row['cUsername'] . "</td>";
echo "<td class='forum'>" . $row['cEmail'] . "</td>";
echo "<td class='forum'>$a</td>";
echo "<td class='forum'>" . $row['uCreateDate'] . "</td>";
echo "</tr>";
}
The value of $a is not overwritten if it does not meet the condition, meaning other iterations may get the value of 1 incorrectly. Here is a fix (replace your if statement):
$a = ($row['cDeviceRegistrationId'] > 0) ? 1 : '';
How about:
if( !is_null($row['cDeviceRegistrationId']) ){
$a = 1;
}
Related
I need to display:
/images/image1.jmp if the number is positive
or
/images/image2.jpg if the number is negative
or
/images/image3 if the number is 0.
$stmt = sqlsrv_query($conn,$sql);
echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
echo "<td>" . $row['']. "</td>";
}
echo "</table>";
?>
I found this code and have tried different ways if incorporating it in the echo "<td>" . $row['']. "</td> but not having any luck.
I get the code but can not manipulate it to fit what it needs to do. I'm sure it's a simple solution. Just frustrated.
switch ($myNumber) {
case 0:
echo "Zero is not a valid value.";
break;
case $myNumber < 0:
echo "Negative numbers are not allowed.";
break;
default:
echo "Great! Ready to make calculations.";
break;
}
Thanks for the help guys. Got the answer that worked.
I think this is a more readable solution.
I use an array to hold the links and use a calculation to see if it is negative or positive.
$img = ["-1" => "/images/image2.jpg",
"0" => "/images/image3.jpg",
"1" => "/images/image1.jpg"];
$number = 0;
Echo ($number == 0 ? $img[$number] : $img[$number/abs($number)]);
https://3v4l.org/JstZl
If the number is positive the calculation will be 15/15 => 1.
If the number is negative -10/10 => -1.
Edit:
$stmt = sqlsrv_query($conn,$sql);
echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";
$img = ["-1" => "/images/image2.jpg",
"0" => "/images/image3.jpg",
"1" => "/images/image1.jpg"];
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
Echo '<td><img src="' . ($row['percentchange'] == 0 ? $img[$row['percentchange']] : $img[$row['percentchange']/abs($row['percentchange'])]) . '"></td>';
}
echo "</table>";
You didn't show us where $myNumber comes from so if the variable name is different just modify it in the code below.
echo '<td>/images/image' . ($myNumber == 0 ? '3' : ($myNumber >= 1 ? '1' : '2')) . '.jpg</td>';
Try:
if($myNumber < 0) {
//show /images/image2.jpg
}
if($myNumber == 0) {
//show /images/image3
}
if($myNumber > 0) {
//show /images/image1.jmp
}
In your code:
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>" . $row['Offense']. "</td>";
echo "<td>" . $row['PreviousDateRange']."</td>";
echo "<td>" . $row['DateRange']."</td>";
echo "<td>" . $row['difference1']."</td>";
echo "<td>" . $row['percentchange']."</td>";
echo "<td>";
if($row['percentchange'] < 0) {
//show /images/image2.jpg
}
if($row['percentchange'] == 0) {
//show /images/image3
}
if($row['percentchange'] > 0) {
//show /images/image1.jmp
}
echo "</td>";
}
This is assuming, based off of your other comments, that $row['percentchange'] is the number in particular that you care about checking against.
I'm struggling to create a table made of the joomla database. Currently I'm using the following code:
$query= "SELECT DATUM, A , B ,C, D, E, F, G FROM ".$db->quoteName('#__XYZTABLE'). " WHERE DATUM_ENG>= CURDATE()";
$db->setQuery($query);
$results = $db -> loadObjectList();
echo "<table><tr><th>Name</th><th>Department</th></tr>";
foreach($results as $row){
echo "<tr>"; echo "<td>".$row->last_name."</td>";
echo "<td>".$row->dept."</td>";
echo "</tr>"; }
echo "</table>";
Basically, this code works fine. The result is:
DATUM , A , B ,C, D, E, F, G
xxxx1 ,aa1,bb1,cc1,dd1,ee1,ff1,gg1
xxxx2 ,aa2,bb2,cc2,dd2,ee2,ff2,gg2
....
However, I want to create an html table which transposes the data and shows it like the following form:
xxxx1 , xxxx2, xxxx3 ....
A aa1 , aa2 , aa3
B bb1 , bb2 ,bb3
C cc1 , cc2 ,cc3
D .....
After searching in this forum and Google, I wasn't able to find anything similar. Can someone help me with a specific code?
Using some php array functions, you may take this:
$object = new stdClass();
// create some test values
$object->DATUM = '2016120';
$object->A = 'aa';
$object->B = 'bb';
$object->C = 'cc';
$object->D = 'dd';
$object->E = 'ee';
$object->F = 'ff';
$object->G = 'gg';
for ($i = 1; $i < 10; $i++){
$vars = get_object_vars($object);
$obj = new stdClass;
foreach ($vars as $key=>$var){
$obj->$key = $var.$i;
}
$results[] = $obj;
}
// end of creating test values
echo "<table><tr><th>DATUM</th><th>A</th><th>B</th><th>C</th><th>D</th></tr>";
foreach ( $results as $row ) {
echo "<tr>";
echo "<td>" . $row->DATUM . "</td>";
echo "<td>" . $row->A . "</td>";
echo "<td>" . $row->B . "</td>";
echo "<td>" . $row->C . "</td>";
echo "<td>" . $row->D . "</td>";
echo "<td>" . $row->E . "</td>";
echo "<td>" . $row->F . "</td>";
echo "<td>" . $row->G . "</td>";
echo "</tr>";
}
echo "</table>";
echo '<h3>New Variant</h3>';
echo '<table>';
$elements = count($results);
foreach (get_object_vars($results[0]) as $key=>$var){
echo '<tr>';
$tag = ($key === 'DATUM') ? 'th>' : 'td>';
echo '<'.$tag . $key .'</'.$tag;
for ($i = 0; $i < $elements; $i++){
echo '<'.$tag . $results[$i]->$key.'</'.$tag;
}
echo '</tr>';
}
echo '</table>';`
I want to use the function onclick to increment a variable [increment the variable paid]
paid is by default at 0 i want to increment at 1.
I try this code : onclick='paid++' but is not working...
My Code
foreach($query as $index=>$row) {
echo ($colorCount++%2) ? "<tr class='alt'>" : "<tr>";
echo "<td class='" . getPaidColor($row['paid']) . "' onclick='paid++ '></td>";
echo "<td class='" . getDelayColor($row['hour']) . "' onclick='postDeleteForm(" . $index . ")'></td>";
foreach($columns as $column) {
echo "<td>" . $row[$column] . "</td>";
}
}
I managed to get the checkboxes to POST the data and displays each on the checkout page but the problem I have is when it gets to 4th column it stops when it hits a certain char limit also how would I turn this into a table on the checkout page.
Database snippet code:
print '<td><input type="checkbox" name="check_list[]"value='. $getColumn[0]. $getColumn[1]. $getColumn[2]. $getColumn[3]. $getColumn[4]. $getColumn[5].$getColumn[6].$getColumn[7].$getColumn[8].$getColumn[9].'</td>';
for ($column = 1; $column < pg_num_fields($res); $column++)
{
print "<td>" . $getColumn[$column] . "</td>";
}
}
print '</table>'
Checkout page
<?php
echo "<hr />\n";
$res = pg_query ($con, "select count(ref) from music");
$a = pg_fetch_row($res);
echo "<p>Total " . $a[0] . " music in database.</p>";
echo "<table border='1'>\n<thead>\n<tr>\n";
echo "<th>Artist</th><th>Composer</th><th>Genre</th><th>Title</th><th>Album</th><th>Label</th> <th>Price</th><th>Description</th>\n";
echo "</tr>\n</thead>\n<tbody>\n";
$res=pg_query($con, "SELECT * from music ORDER BY ref");
while ($a = pg_fetch_array ($res))
{
echo "<tr>";
for ($j = 0; $j < pg_num_fields($res); $j++) {
// htmlspecialchars converts things like & to HTML entity codes
echo "<td>" . htmlspecialchars($a[$j], ENT_QUOTES) . "</td>";
}
echo "</tr>\n";
}
echo "</tbody>\n</table>";
?>
I am sure, you are tried to do the following:
print '<table>';
for ($column = 1; $column < pg_num_fields($res); $column++) {
echo '<tr>';
print '<td><input type="checkbox" name="check_list[]" value="'.$getColumn[$column] .'" /></td>';
print "<td>" . $getColumn[$column] . "</td>";
echo '</tr>';
}
print '</table>';
I got code snippet from this site, which I used as shown below
$data = array();
while($row = mysql_fetch_assoc($num1)) {$data['row'][] = $row;}
while($row = mysql_fetch_assoc($num2)) {$data['row2'][] = $row;}
$count = count($data['row']);
echo "<table>" ;
echo "<tr>";
echo "<td width='300' bgcolor='#99CCF5' align='Left' style='padding-left:30px'><b>Country</b></td>" ;
echo "<td width='150' bgcolor='#99CCF5' align='center'><b>Mid Estimate 1</b></td>";
echo "<td width='150' bgcolor='#99CCF5' align='center'><b>Mid Estimate 2</b></td>";
echo "</tr>";
for($i=0;$i<=$count;$i++)
{
if(($i % 2) == 1)
{
echo "<tr>" ;
echo "<td align='center'>" . $data['row'][$i]['Country']."</td>";
echo "<td align='center'>" . $data['row'][$i]['MidEstimate']."</td>";
echo "<td align='center'>" . $data['row2'][$i]['MidEstimate']."</td>";
echo "</tr>" ;
}else
{
echo "<tr>" ;
echo "<td align='center'>" . $data['row'][$i]['Country'] ."</td>";
echo "<td align='center'>" . $data['row'][$i]['MidEstimate']."</td>";
echo "<td align='center'>" . $data['row2'][$i]['MidEstimate']."</td>";
echo "</tr>" ;
}
}
echo "</table>" ;
which gives a reult like below
image1 http://img4.pixa.us/8ba/19338641.jpg
where the correct result should be like this
image2 http://img4.pixa.us/c1d/19338642.jpg
that is if any value in a column is empty the next adjucent value gets that position. How can I make this correct? that is if any value is empty that column must be empty.
please help and thanks in advance.
You have to gather the data for each country. Your approach in the question messes up the listing since the keys for the array are not in sync. Let's sync your rows by 'Country':
$data = array();
while($row = mysql_fetch_assoc($num1))
{
$c = $row['Country'];
if (!isset($data[$c]))
{
$data[$c] = array('Country' => $c);
}
$data[$c]['MidEstimate1'] = $row['MidEstimate'];
}
while($row = mysql_fetch_assoc($num2))
{
$c = $row['Country'];
if (!isset($data[$c]))
{
$data[$c] = array('Country' => $c);
}
$data[$c]['MidEstimate2'] = $row['MidEstimate'];
}
Now you have a row in your array for every Country, with their data from each query.
$i = 0;
foreach ($data as $row)
{
echo ($i % 2) ? "<tr class='odd'>" : "<tr class='even'>" ;
echo "<td align='center'>" . $row['Country']."</td>";
echo "<td align='center'>" . $row['MidEstimate1']."</td>";
echo "<td align='center'>" . $row['MidEstimate2']."</td>";
echo "</tr>" ;
}
Note: this only works in 'Country' field is present in both SQL query.