php generated td overlaps data when data is longer than column width - php

I have the following generated column for "Detail"
echo '<td width="100" align="left" bgcolor="#FFFFFF">Cheque (' . $row4['number'] . ')'. $row1['date_added'] .'</td>';
which gives me overall following table of results, where the particular column data overlaps when it is too long. What should I do? I have tried adding a '<br/>' in between, but it never worked.
Unfortunately this table had no external CSS file but the following is the full table
<table cellspacing="1" width="900" cellpadding="3" bgcolor="#CCCCCC" style="line-height:0px;margin-left: auto;margin-right: auto">
<col width="64" span="10" />
<tr height="46">
<td height="46" width="600" colspan="10" bgcolor="#FFFFFF"><h1>Blah Blah</h1></td>
</tr>
<?php
include './DatabaseConnection.php';
$db = new DatabaseConnection();
$db->createDatabaseConnection();
$cusid = $_GET['cusid'];
$startDate=$_GET['startDate'];
$endDate=$_GET['endDate'];
$query2 = mysql_query("select cusname from customers where cusid='$cusid'");
while ($row5 = mysql_fetch_array($query2)) {
echo '<tr height="20" width="1000">
<td height="10" colspan="10" bgcolor="#F3F3F3"><h2>Payment History of ' . $row5['cusname'] . ' (Credit)</h2></td>
</tr><tr height="20">
<td width="120" height="20" bgcolor="#FFFFFF"><h3>Date Added</h3></td>
<td width="120" bgcolor="#FFFFFF"><h3 align="center">Invoice Number</h3></td>
<td width="120" bgcolor="#FFFFFF"><h3 align="center">Invoice Amount</h3></td>
<td width="120" bgcolor="#FFFFFF"><h3 align="center">Payed Amount</h3></td>
<td width="120" bgcolor="#FFFFFF"><h3 align="center">Detail</h3></td>
<td width="120" bgcolor="#FFFFFF"><h3 align="center">Balance</h3></td>
</tr>';
}
//$query = mysql_query("SELECT * from payments where cusid ='$cusid' and ingrtype = '1' and payment_status = '1' and (date_added >= '$startDate' and date_added <= '$endDate') order by invoiceGRN_id") or die(mysql_error());
$query = mysql_query("SELECT * from payments where cusid ='$cusid' and ingrtype = '1' and payment_status = '1' and (date_added >= '$startDate' and date_added <= '$endDate') order by invoiceGRN_id") or die(mysql_error());
$lastinvoicenumber = null;
while ($row1 = mysql_fetch_array($query)) {
$invoiceAmount = $row1['subtotal'];
$payedAmount = $row1['payment'];
$invoicenumber = $row1['invoiceGRN_id'];
$invoicetotal = 0;
//$balanceAmount = $invoiceAmount - $payedAmount;
$balanceAmount = $invoiceAmount;
//diluk
$queryinv = mysql_query("SELECT invoice_subtotal from invoice where invoice_number ='$invoicenumber' limit 1") or die(mysql_error());
//diluk
while ($rowinv = mysql_fetch_array($queryinv)) {
$invoicetotal = $rowinv['invoice_subtotal'];
}
echo '<tr height="20">
<td width="100" align="left" bgcolor="#FFFFFF">' . $row1['date_added'] . '</td>';
if($lastinvoicenumber!=$invoicenumber){
echo '<td width="100" height="20" bgcolor="#FFFFFF">' . $row1['invoiceGRN_id'] . '</td>';
echo '<td width="100" align="right" bgcolor="#FFFFFF">' . $invoicetotal . '</td>';
}else{
echo '<td width="100" height="20" bgcolor="#FFFFFF"></td>';
echo '<td width="100" align="right" bgcolor="#FFFFFF"></td>';
}
echo '<td width="100" align="right" bgcolor="#FFFFFF">' . $row1['payment'] . '</td>';
$invId = $row1['invoiceGRN_id'];
if ($row1['paymentType'] == "Cheque") {
$query3 = mysql_query("select * from cheque where invoiceno='$invId'");
if ($row4 = mysql_fetch_array($query3)) {
echo '<td width="100" align="left" bgcolor="#FFFFFF">Cheque (' . $row4['number'] . ')'. $row1['date_added'] .'</td>';
}else{
echo '<td width="100" align="left" bgcolor="#FFFFFF">Cheque -' . $row1['date_added'] .'</td>';
}
} else {
echo '<td width="100" align="left" bgcolor="#FFFFFF">' . $row1['paymentType'] . '</td>';
}
echo '<td width="100" align="right" bgcolor="#FFFFFF">' . $balanceAmount . '</td>
</tr>';
$lastinvoicenumber = $invoicenumber;
}
echo '<tr height="20" width="1000">
<td height="10" colspan="10" bgcolor="#F3F3F3"><h2></h2></td></tr>';
$queryx = mysql_query("SELECT * from invoice where customer_id ='$cusid' and payment_type !='Credit' group by invoice_number order by invoice_date") or die(mysql_error());
while ($rowx = mysql_fetch_array($queryx)) {
echo '<tr height="20">
<td width="100" align="left" bgcolor="#FFFFFF">' . $rowx['invoice_date'] . '</td>
<td width="100" height="20" bgcolor="#FFFFFF">' . $rowx['invoice_number'] . '</td>
<td width="100" height="20" bgcolor="#FFFFFF">' . $rowx['invoice_subtotal'] . '</td>
<td width="100" height="20" bgcolor="#FFFFFF">' . $rowx['invoice_subtotal'] . '</td>';
$invId1 = $rowx['invoice_number'];
if ($rowx['payment_type'] == "Cheque") {
$queryx1 = mysql_query("select * from cheque where invoiceno='$invId1'");
if ($rowx2 = mysql_fetch_array($queryx1)) {
echo '<td width="100" align="left" bgcolor="#FFFFFF">Cheque (' . $rowx2['number'] . ')</td>';
}
} else {
echo '<td width="100" align="left" bgcolor="#FFFFFF">' . $rowx['payment_type'] . '</td>';
}
echo '<td width="100" align="right" bgcolor="#FFFFFF">0</td>
</tr>';
}
?>
</table>

There are 2 things you have to consider:
1. table-layout: fixed; this for table, and
2. word-wrap: break-word; to td itself.
For detailed see this question: How to force table cell <td> content to wrap?

I have also faced the same problem, just add \n to the end of every <tr> tag like this
echo "<tr><td></td><td></td></tr> \n"

you can check
echo '<td width="100" align="left" bgcolor="#FFFFFF"><div style="width:200px; word-wrap: break-word;overflow: auto;">Cheque (' . $row4['number'] . ')'. $row1['date_added'] .'</div></td>';

I solved the isue by making the table's "line-height" attribute to 20px like below,
<table cellspacing="1" width="900" cellpadding="3" bgcolor="#CCCCCC" style="line-height:20px;margin-left: auto;margin-right: auto">

Related

Change the background Color of the cell according to $diff

I want to make a date filter page where one can filter the data stored in the database according to the range of dates.
I have a column "DIFF" which stores the time difference of Intime and Outtime.
I want to change the background color of the cell according to the value stored in $diff. If it is less than 8 the red color else green.
I am not able to do this..
please help
$result = mysqli_query($connect, $query);
$cnt=1;
$output .= '
<table class="table table-bordered">
<tr>
<th width="8%">Sr No.</th>
<th width="18%">EMPLOYEE ID</th>
<th width="30%">EMPLOYEE NAME</th>
<th width="15%">IN_TIME</th>
<th width="15%">OUT_TIME</th>
<th width="20%">DATE</th>
<th width="20%">WORKING HOURS</th>
</tr>
';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$outtime = $row["outtime"];
$intime = $row["intime"];
$array1 = explode(':', $intime);
$array2 = explode(':', $outtime);
$minutes1 = ($array1[0] * 60.0 + $array1[1])/60;
$minutes2 = ($array2[0] * 60.0 + $array2[1])/60;
$diff = round(($minutes2 - $minutes1),1).' hrs';
$output .= '
<tr>
<td align="center">'. $cnt .'</td>
<td align="center">'. $row["EmpId"] .'</td>
<td align="center">'. $row["FirstName"] . " " .
$row["LastName"].'</td>
<td align="center">'. $row["intime"] .'</td>
<td align="center"> '. $row["outtime"] .'</td>
<td align="center">'. $row["date"] .'</td>
<td align="center" > '.$diff.' </td>
</tr>
'; $cnt++;
}
}
else
{
$output .= '
<tr>
<td colspan="5">No Record Found</td>
</tr>
';
}
$output .= '</table>';
echo $output;
}
Try adding background-color property for td that you want to fill color.
Code should look like:
$output .= '<tr>
<td align="center">' . $cnt . '</td>
<td align="center">' . $row["EmpId"] . '</td>
<td align="center">' . $row["FirstName"] . " " . $row["LastName"] . '</td>
<td align="center">' . $row["intime"] . '</td>
<td align="center"> ' . $row["outtime"] . '</td>
<td align="center">' . $row["date"] . '</td>';
if ($diff < 8) {
$output .= '<td align="center" style="background-color: #ff0000;"> ' . $diff . ' </td>';
} else {
$output .= '<td align="center" style="background-color: #00ff00;"> ' . $diff . ' </td>';
}
$output .= '</tr>';
Have you tried do a less than operation on the $diff variable?
You can try this, it sets the background color of the td depends on the $diff value.
change this part:
<td align="center" > '.$diff.' </td>
to this
<td align="center" style="background-color: ($diff < 8) ? 'red' : 'green'" > '.$diff.' </td>
check $diff is less then 8 or not
use below code
<?php
$result = mysqli_query($connect, $query);
$cnt=1;
$output .= '
<table class="table table-bordered">
<tr>
<th width="8%">Sr No.</th>
<th width="18%">EMPLOYEE ID</th>
<th width="30%">EMPLOYEE NAME</th>
<th width="15%">IN_TIME</th>
<th width="15%">OUT_TIME</th>
<th width="20%">DATE</th>
<th width="20%">WORKING HOURS</th>
</tr>
';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$outtime = $row["outtime"];
$intime = $row["intime"];
$array1 = explode(':', $intime);
$array2 = explode(':', $outtime);
$minutes1 = ($array1[0] * 60.0 + $array1[1])/60;
$minutes2 = ($array2[0] * 60.0 + $array2[1])/60;
$diff = round(($minutes2 - $minutes1),1).' hrs';
$output .= '
<tr>
<td align="center">'. $cnt .'</td>
<td align="center">'. $row["EmpId"] .'</td>
<td align="center">'. $row["FirstName"] . " " .
$row["LastName"].'</td>
<td align="center">'. $row["intime"] .'</td>
<td align="center"> '. $row["outtime"] .'</td>
<td align="center">'. $row["date"] .'</td>
<td align="center" ';
if($diff < 8)
{
$output .= 'bgcolor="red"';
}
else
{
$output .= 'bgcolor="green"';
}
$output .= ' > '.$diff.' </td>
</tr>
'; $cnt++;
}
}
else
{
$output .= '
<tr>
<td colspan="5">No Record Found</td>
</tr>
';
}
$output .= '</table>';
echo $output;
?>
You can use ternary operators as
$diff = round(($minutes2 - $minutes1),1).' hrs';
$colorCode = ($diff < 8)?"#ff0000":"#00ff00";
$output .= '
<tr>
<td align="center">'. $cnt .'</td>
<td align="center">'. $row["EmpId"] .'</td>
<td align="center">'. $row["FirstName"] . " " .
$row["LastName"].'</td>
<td align="center">'. $row["intime"] .'</td>
<td align="center"> '. $row["outtime"] .'</td>
<td align="center">'. $row["date"] .'</td>
<td align="center" style="background-color:$colorCode"> '.$diff.' </td>
</tr>
';
you can do the same by using php like :-
<td align="center"<?php if($diff<8){?> style="background-color: red; " <?php }else{?>style="background-color: green;"<?php } ?> > '.$diff.' </td>
or by using js too like :-
<td align="center" class="check" > '.$diff.' </td>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
//alert($('.check').text());
$(" tr > td.check").each(function(){
if($(this).text()<8){
$( this ).css( "background-color", "red" );
}else{
$( this ).css( "background-color", "green" );
}
});
});
</script>

PHP, Array result in table format in a sequence [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm using below coding to get an array result in a table,
$presents = #json_decode(fBGetDataStore('presents'), true);
foreach ($presents as $key=>$value){
$icon = $_SESSION['data']->table('units')->byCode($value['itemCode'])->data();
$i = 1;
echo '<table border=1 class="sortable" align="center">
<tr>
<td class="sortable" align="center">#</td>
<td class="sortable" align="center">Sender IMG</td>
<td class="sortable" align="center">Sender ID</td>
<td class="sortable" align="center">Item Code</td>
<td class="sortable" align="center">IMG</td>
</tr>';
echo '<tr><td align="center">'.$i++.'</td>';
echo '<td> <img src="http://graph.facebook.com/'. $value['sender'] .'/picture" width="70" height="50" </td>';
echo '<td align="center">'. $value['sender'] . '</td>';
echo '<td align="center">'. $value['itemCode'] . '</td>';
echo '<td><img src="http://static-0.farmville.zgncdn.com/assets/hashed/' . fBImageGetHash($icon['iconname']). '" width=45px height=45px ></td>' ;
echo '</tr>';
}
and also I'm getting a table but I want to count # of results in a sequence because right now my results are showing in many more than one tables which contain only one row.
I'm getting as a result :
echo '<table border=1 class="sortable" align="center">
<tr>
<td class="sortable" align="center">#</td>
<td class="sortable" align="center">Sender IMG</td>
<td class="sortable" align="center">Sender ID</td>
<td class="sortable" align="center">Item Code</td>
<td class="sortable" align="center">IMG</td>
</tr>';
$presents = #json_decode(fBGetDataStore('presents'), true);
foreach ($presents as $key=>$value){
$icon = $_SESSION['data']->table('units')->byCode($value['itemCode'])->data();
$i = 1;
echo '<tr><td align="center">'.$i++.'</td>';
echo '<td> <img src="http://graph.facebook.com/'. $value['sender'] .'/picture" width="70" height="50" </td>';
echo '<td align="center">'. $value['sender'] . '</td>';
echo '<td align="center">'. $value['itemCode'] . '</td>';
echo '<td><img src="http://static-0.farmville.zgncdn.com/assets/hashed/' . fBImageGetHash($icon['iconname']). '" width=45px height=45px ></td>' ;
echo '</tr>';
}
you need to print the table outside of loop, so only the relevent row will repeat itself.
just tested the 1st and 2nd answer (by both of them you will get 1 means same figure as # column so my opinion is you should use to count rows :
$presents = #json_decode(fBGetDataStore('presents'), true);
$i = 1;
// Table starting
echo '<table border=1 class="sortable" align="center">
<tr>
<td class="sortable" align="center">#</td>
<td class="sortable" align="center">Sender IMG</td>
<td class="sortable" align="center">Sender ID</td>
<td class="sortable" align="center">Item Code</td>
<td class="sortable" align="center">IMG</td>
</tr>';
foreach ($presents as $key=>$value){
$icon = $_SESSION['data']->table('units')->byCode($value['itemCode'])->data();
echo '<tr><td align="center">'.$i++.'</td>';
echo '<td> <img src="http://graph.facebook.com/'. $value['sender'] .'/picture" width="70" height="50" </td>';
echo '<td align="center">'. $value['sender'] . '</td>';
echo '<td align="center">'. $value['itemCode'] . '</td>';
echo '<td><img src="http://static-0.farmville.zgncdn.com/assets/hashed/' . fBImageGetHash($icon['iconname']). '" width=45px height=45px ></td>' ;
echo '</tr>';
}
echo '</table>'; //Table end
i hope you will get it as your need.

Three Column Population

So, I took over a project from someone else, and I am trying to populate three columns in a table where there was previously two columns. I've almost got things figured out, but can't quite get my third column to fill. I've got it so its separating the the data into thirds, but only the left column actually has exactly one third of the data. The middle column has the other two thirds of data, and the right column has no data.
Here is the code I am working with. However, I've changed what we produce to "stuff" and left out the url for privacy reasons:
$iUp = 1;
$pQuery = mysql_query($query) or die(mysql_error());
$pExist = mysql_num_rows($pQuery);
if(!empty($pExist)){
//$this->stuffPackageOut .= '<form action="https://*************.111/cart" method="GET" name="stuff" onsubmit="return anyCheck()">';
//$this->stuffPackageOut .= '<form action="index.php" method="GET" name="stuff" onsubmit="return anyCheck()">';
$this->stuffPackageOut .= '<input type="hidden" name="cart!addFamily">';
$pCnt = $pExist;
$pCntHalf = floor($pCnt/3);
$stuff[0] = NULL;
$stuff[1] = NULL;
$stuff[2] = NULL;
$div = 0;
while($pRow = mysql_fetch_object($pQuery)){
if($pCntHalf < $iUp){
$div = 2;
$div = 1;
}else{
$div = 0;
}
$contribName = strtolower(str_replace(" ", "", $pRow->conName));
$stuffName = strtolower(str_replace(" ", "", $pRow->stfName));
$stuff[$div] .= '<tr><td valign="top"><input type="checkbox" name="cart_family'.$iUp.'" value="'.$pRow->familyId.'" id="package"> <a href="http://****************.com/stuff/'.$stuffName.'.php" class="packagestufflinks">'.$pRow->fntName.'</td></tr>';
$iUp++;
}
$stuffLeft = '<table width="137" border="0" cellspacing="8" cellpadding="0" height="10" valign="top">';
$stuffLeft .= $stuff[0];
$stuffLeft .= '</table>';
$stuffMiddle = '<table width="137" border="0" cellspacing="8" cellpadding="0" height="10" valign="top">';
$stuffMiddle .= $stuff[1];
$stuffMiddle .= '</table>';
$stuffRight = '<table width="137" border="0" cellspacing="8" cellpadding="0" height="10" valign="top">';
$stuffRight .= $stuff[2];
$stuffRight .= '</table>';
$this->stuffPackageOut .= '<table width="100%" height="10"><tr><td align="left" valign="top" width="238" height="10">'.$stuffLeft.'</td><td width="29" valign="top" background="verticaldivider.gif"><img src="http://www.****************.com/pageimages/spacer.GIF" width="29" height="5"></td><td align="left" valign="top" width="238">'.$stuffMiddle.'<td><td width="29" valign="top" background="verticaldivider.gif"><img src="http://www.****************.com/pageimages/spacer.GIF" width="29" height="5"></td><td align="left" valign="top" width="238">'.$stuffRight.'</td></tr></table>';
I know it has something to do with everything following the "while" coding, but since I don't do this for a living I have no idea what coding is necessary to make my three columns populate.
Please help me.
Try this... it should work for you... used this logic before and worked well:
<?php
$iUp = 1;
$pQuery = mysql_query($query) or die(mysql_error());
$pExist = mysql_num_rows($pQuery);
if(!empty($pExist)){
$this->stuffPackageOut .= '<input type="hidden" name="cart!addFamily">';
$pCnt = $pExist;
$pCntHalf = floor($pCnt/3);
while($pRow = mysql_fetch_object($pQuery)){
$contribName = strtolower(str_replace(" ", "", $pRow->conName));
$stuffName = strtolower(str_replace(" ", "", $pRow->stfName));
if($iUp <= $pCntHalf){
$stuff_left_a .= '<tr><td valign="top"><input type="checkbox" name="cart_family'.$iUp.'" value="'.$pRow->familyId.'" id="package"> <a href="http://****************.com/stuff/'.$stuffName.'.php" class="packagestufflinks">'.$pRow->fntName.'</td></tr>';
}
elseif($iUp >= $pCntHalf +1 && $iUp <= ($pCntHalf * 2)){
$stuff_middle_a .= '<tr><td valign="top"><input type="checkbox" name="cart_family'.$iUp.'" value="'.$pRow->familyId.'" id="package"> <a href="http://****************.com/stuff/'.$stuffName.'.php" class="packagestufflinks">'.$pRow->fntName.'</td></tr>';
}
elseif($iUp >= ($pCntHalf *2) +1){
$stuff_right_a .= '<tr><td valign="top"><input type="checkbox" name="cart_family'.$iUp.'" value="'.$pRow->familyId.'" id="package"> <a href="http://****************.com/stuff/'.$stuffName.'.php" class="packagestufflinks">'.$pRow->fntName.'</td></tr>';
}
$iUp++;
}
$stuffLeft = '<table width="137" border="0" cellspacing="8" cellpadding="0" height="10" valign="top">';
$stuffLeft .= $stuff_left_a;
$stuffLeft .= '</table>';
$stuffMiddle = '<table width="137" border="0" cellspacing="8" cellpadding="0" height="10" valign="top">';
$stuffMiddle .= $stuff_middle_a;
$stuffMiddle .= '</table>';
$stuffRight = '<table width="137" border="0" cellspacing="8" cellpadding="0" height="10" valign="top">';
$stuffRight .= $stuff_right_a;
$stuffRight .= '</table>';
$this->stuffPackageOut .= '<table width="100%" height="10">
<tr>
<td align="left" valign="top" width="238" height="10">'.$stuffLeft.'</td>
<td width="29" valign="top" background="verticaldivider.gif">
<img src="http://www.****************.com/pageimages/spacer.GIF" width="29" height="5">
</td>
<td align="left" valign="top" width="238">'.$stuffMiddle.'<td>
<td width="29" valign="top" background="verticaldivider.gif">
<img src="http://www.****************.com/pageimages/spacer.GIF" width="29" height="5">
</td>
<td align="left" valign="top" width="238">'.$stuffRight.'</td>
</tr>
</table>';
?>
I found the error... it was in my math in the middle... notice the change:
elseif($iUp >= $pCntHalf +1 && $iUp <= ($pCntHalf * 2))

how to post database fetch values from one page to anthor page using HTML table

in this code
"<td align="left">' .$row['o_id'] .'</td>"
i generate multiple o_id so when i click on other particular o_id this o_id post by other page and on that behalf i can access this o_id in multiple pages but i m trying to use hidden type but it not working so any other process to post this value other page....
it is possible to send this so please help me....
and one more thing i using hidden type but is not working also how it's work for post the value from one page to other page using html in php tag....
$sql1 = " select count(status) as count from bill_generation_request where hotel_id = '$hid' and status ='yes'";
$result = mysql_query($sql1);
while ($row = mysql_fetch_array($result))
{
$count = $row['count'];
}
echo '<table align="center" cellspacing="0" cellpadding="5" width="0%" border="1"> <tr>
<td align="left"><b>Bill Request Count</b></td>';
echo '<td align="left">' .$count . '</td> ';
echo '</tr>';
echo '</table>';
echo '<table align="center" cellspacing="0" cellpadding="5" width="75%" border="1">
<tr>
<td align="left"><b>Order Number</b></td>
<td align="left"><b>Table Number</b></td>
<td align="left"><b>Item Count</b></td>
<td align="left"><b>Order Time</b></td>
<td align="left"><b>Total Amount</b></td>
<td align="left"><b>Order Note</b></td>
<td align="left"><b>Note</b></td>
</tr> ';
$i=0;
while ($row = mysql_fetch_array($res))
{
$oid = $row['o_id'];
echo '<tr bgcolor="' . $bg . '">
<td align="left">' .$row['o_id'] .'</td>
<td align="left">' . $row['tableid'] . '</td>
<td align="left">' . $row['total_count'] . '</td>
<td align="left">' .$row['time'] . '</td>
<td align="left">' .$row['amount'] . '</td>
<td align="left">' .$row['onote'] . '</td>
<td align="left">' .$row['note'] . '</td>
</tr>';
$i++;
}
echo '</table>';
mysql_close($con);
Try like
$id = $row['o_id'];
<td align="left">' .$row['o_id'] .'</td>
and Use $_GET['id'] to get that id
Try this
$id = $row['o_id'];
<td align="left">' .$row['o_id'] .'</td>

Display specific image depending on specific text in MySQL table using PHP

In my MySQL database I have table named game with columns: sport_cat, timelive, match_title, selection,
I am exactly this MySQL query SELECT statement for displaying them in grid view on site:
<?
$qry = "
SELECT sport_cat,match_title,selection,
CASE
WHEN game.result LIKE '' THEN 'PENDING'
WHEN game.result LIKE game.selection THEN 'WON'
WHEN game.result NOT LIKE game.selection THEN 'LOST'
END AS result
FROM game
";
$searchText = "";
if($_REQUEST['search_text']!=""){
$searchText = mysql_real_escape_string($_REQUEST['search_text']);
$qry .=" WHERE sport_cat.timelive LIKE '%$searchText%' " .
" OR game.timelive LIKE '%$searchText%' " .
" OR game.match_title LIKE '%$searchText%' ";
}
$qry .= " ORDER BY timelive DESC";
$obj = new pagination_class($qry,$starting,$recpage);
$result = $obj->result;
?>
In first column sport_cat there are few possible text inputs: football, tennis, basketball.
HTML of my file where this part is looks like this:
<table>
<?if(mysql_num_rows($result)!=0){
$counter = $starting + 1;
while($data = mysql_fetch_array($result)) {?>
<tr>
<td align="center"><? echo $data['sport_cat']; ?></td>
<td align="center"><? echo $data['timelive']; ?></td>
<td align="center"><? echo $data['match_title']; ?></td>
<td align="center"><? echo $data['selection']; ?></td>
</tr>
<?
$counter ++;
} ?>
</table>
What I am trying to do is to display specific picture in those cell according to that input in cell. So if there is football want to display football.png picture or if there is tennis want to display tennis.png or if there is basketball want to display basketball.png.
Also, if there is maybe some unknown input like cricket and don't have picture specified for that word would like to display unknown.png picture.
Pictures are stored under images/icons on my server.
Now i'm not sure how to implement this code regarding picturing issue into this already existent i have.
Thanks.
ADDED full example code: 03.11.2011.:
<?php
error_reporting(E_ALL^E_NOTICE);
include('pagination_class.php');
mysql_connect('xxx.xxx.xxx.xx', 'xxxxxx', 'xxxxxxxx') or die(mysql_error());
mysql_select_db('xxxxxxx');
?>
<script language="JavaScript" src="pagination.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
<?
$qry = "
SELECT sport_cat,bidprice,timelive,match_title,selection,winnings_with_startamount,odds,odds*10 AS gainedodds,username,
CASE
WHEN game.result LIKE '' THEN 'pending'
WHEN game.result LIKE game.selection THEN 'WON'
WHEN game.result NOT LIKE game.selection THEN 'LOST'
END AS result
FROM game
";
$searchText = "";
if($_REQUEST['search_text']!=""){
$searchText = mysql_real_escape_string($_REQUEST['search_text']);
$qry .=" WHERE game.sport_cat LIKE '%$searchText%' " .
" OR game.timelive LIKE '%$searchText%' " .
" OR game.match_title LIKE '%$searchText%' " .
" OR game.selection LIKE '%$searchText%' " .
" OR game.username LIKE '%$searchText%'";
}
$qry .= " ORDER BY timelive DESC";
//for pagination
$starting=0;
$recpage = 10;//number of records per page
$obj = new pagination_class($qry,$starting,$recpage);
$result = $obj->result;
function getStyleColorForStatus($status) {
if ($status == 'WON') {
return '#99ff99';
}
else if ($status == 'LOST') {
return '#ff9999';
}
else if ($status == 'pending') {
return '#e5e5e5';
}
return '';
}
?>
<form name="form1" action="testpage.php" method="POST" style="background-color:#f9f9f9; -webkit-box-shadow: 0 1px 10px rgba(0,0,0,.1); -moz-box-shadow: 0 1px 10px rgba(0,0,0,.1); box-shadow: 0 1px 10px rgba(0,0,0,.1);">
<table border="0" align="center" width="1000px" padding-left="0px" style="background:#F9F9F9;">
<TD colspan="0" style="padding-left:0px; padding-bottom:5px; padding-top:10px; text-align:center;">
Search: <input type="text" name="search_text" value="<?php echo $searchText; ?>">
<input type="submit" value="Search">
</TD>
<tr><TD colspan="0">
<div id="page_contents">
<table width="968px" cellspacing="0" cellpadding="5" align="center" frame="box" rules="none" style="padding-bottom:20px; margin-bottom:8px;margin-top:3px; -webkit-box-shadow: 0 1px 10px rgba(0,0,0,.1); -moz-box-shadow: 0 1px 10px rgba(0,0,0,.1); box-shadow: 0 1px 10px rgba(0,0,0,.1); border: 1px solid #cccccc;">
<tr style="height: 50px;">
<td width="10%" align="center" td class="winheader"><div class="glowtext">Event Category</div></td></td>
<td width="5%" align="center" td class="winheader"><div class="glowtext">Bid Cost</div></td></td>
<td width="10%" align="center" td class="winheader"><div class="glowtext">Event Start Time</div></td></td>
<td width="35%" align="center" td class="winheader"><div class="glowtext">Market/Event</div></td></td>
<td width="10%" align="center" td class="winheader"><div class="glowtext">Selection</div></td></td>
<td width="10%" align="center" td class="winheader"><div class="glowtext">Winnings</div></td></td>
<td width="5%" align="center" td class="winheader"><div class="glowtext">Odds</div></td></td>
<td width="5%" align="center" td class="winheader"><div class="glowtext">Gained Odds</div></td></td>
<td width="10%" align="center" td class="winheader"><div class="glowtext">Winning Bidder</div></td></td>
<td width="10%" align="center" td class="winheader"><div class="glowtext">Final Result</div></td></td>
</tr>
<?
if(mysql_num_rows($result)!=0){
$counter = $starting + 1;
$pathImg = "/images/icons/";
while($data = mysql_fetch_array($result)) {
//calculate url image
$pathFile = $pathImg . $data['sport_cat'] . ".png";
if (!file_exists($pathFile)) {
$pathFile = $pathImg . "unknown.png";
}
?>
<tr class="initial" onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'">
<td align="center"><img src="<?=$pathFile?>" alt="<?=$data['sport_cat']?>"></TD>
<td align="center"><? echo $data['bidprice']; ?></TD>
<td align="center"><? echo $data['timelive']; ?></TD>
<td align="left"><? echo $data['match_title']; ?></TD>
<td align="left"><? echo $data['selection']; ?></TD>
<td align="center"><font color="green">€ <? echo $data['winnings_with_startamount']; ?></TD>
<td align="center"><? echo $data['odds']; ?></TD>
<td align="center"><? echo $data['gainedodds']; ?></TD>
<td align="center"><? echo $data['username']; ?></TD>
<td align="center" style="background-color:<?php echo getStyleColorForStatus($data['result']); ?>"><? echo $data['result']; ?></td>
</tr>
<?
$counter ++;
}
}
?>
<tr><TD align="center" colspan="10" style="padding-bottom:1px; padding-top:10px; color:#333;"><? echo $obj->anchors; ?></TD></tr>
<tr><TD align="center" colspan="10" style="padding-bottom:10px; padding-top:5px; color:#333;"><? echo $obj->total; ?></TD></tr>
<?}else{?>
<tr><TD align="center" colspan="10" style="padding-bottom:10px padding-top:10px; color:red;">No Data Found</TD></tr>
<?}?>
</TD></tr>
</table>
</div>
</tr>
</TD>
</form>
</table>
try this:
$sql = "SELECT * FROM game";
$result = mysql_query($sql) or die(mysql_error());
$pathImg = "/images/icons/";
while ($row = mysql_fetch_array($result)) {
$pathFile = $pathImg . $row['sport_cat'] . ".png";
if (!file_exists($pathFile)) {
$pathFile = $pathImg . "unknown.png";
}
echo '<img src="' . $pathFile . '">';
}
You edit Ask and I edit Anwer :P
<table>
<?
if (mysql_num_rows($result) != 0) {
$counter = $starting + 1;
$pathImg = "/images/icons/";
while ($data = mysql_fetch_array($result)) {
//calculate url image
$pathFile = $pathImg . $data['sport_cat'] . ".png";
if (!file_exists($pathFile)) {
$pathFile = $pathImg . "unknown.png";
}
?>
<tr>
<td align="center"><img src="<?=$pathFile?>" alt="<?=$data['sport_cat']?>"></td>
<td align="center"><? echo $data['sport_cat']; ?></td>
<td align="center"><? echo $data['timelive']; ?></td>
<td align="center"><? echo $data['match_title']; ?></td>
<td align="center"><? echo $data['selection']; ?></td>
</tr>
<?
$counter++;
}
}
?>
</table>
Something like the following should work.
<?php
$sql = "SELECT * FROM game";
$result = mysql_query($sql)or die(mysql_error());
while ($row = mysql_fetch_array($result)){
// Show images
if($row['sport_cat']=='football'){
print "<img src='football.png'/>";
}
elseif($row['sport_cat']=='basketball'){
print "<img src='basketball.png'/>";
}
elseif($row['sport_cat']=='tennis'){
print "<img src='tennis.png'/>";
}else{
print "<img src='unknown.png' />";
}
} // End while loop
?>

Categories