Display specific image depending on specific text in MySQL table using PHP - 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
?>

Related

No error but no ITEM is showing out

Hello this is the code in my php, i dont get error but the items in the database doesn't show and only the else tag is execute.
<?php
$sql = $bdd->prepare('SELECT * FROM bgk9z_items');
if ($sql->fetchColumn() > 0) { //
while ($result = $sql->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<th id="checkbox"><input type="checkbox" name="toggle" /></th>
<td id="titre"><? echo $result['title']; ?>
<div class="opt"><span class="up">Modifier</span> <span class="del">Supprimer</span> <span class="sho">Afficher</span></div>
</td>
<td id="extrait"><? $string = $result['introtext']; $long = (strlen($string) > 13) ? substr($string,0,280).'...' : $string; echo $long;?></td>
<td id="cat"><? include_once(dirname(__DIR__) . '/controlers/show-cat-name.php'); ?></td>
<td id="etat"><? $rep = $result['published']; if ($rep == 1) { echo "Oui";} else{echo "Non";} ?></td>
<td id="image"><? echo "Image"; ?></td>
<td id="ident"><? echo $result['id']; ?></td>
</tr>
<?
}
}
else { ?>
<tr><td colspan="7" style="text-align: center; color: rgb(192, 0, 0); font-size: 23px; font-weight: bold;">NO ITEM TO SHOW</td></tr>
<?php }
?

Show checkbox with database value

I am having trouble with a part of my code. I want the checkbox in the last column to be checked if the corresponding DB value is 1, but something keeps going wrong with my code, anyone who sees what's wrong? It's probably very simple but I can't find it.
<?php
$sql = "SELECT * FROM registered ORDER BY datum";
$myData=mysql_query($sql,$con) ;
?>
<table width="1100" border="1">
<tr>
<th style="text-align:center; padding:0 10px">naam</th>
<th style="text-align:center; padding:0 10px">betaald?</b></th>
</tr>
<?php
$betaald = $record['betaald'];
while($record = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>" . $record['naam'] . "</td>";
echo "<td> <input type='checkbox' name='betaald' id='betaald' value='1' ". echo ($betaald==1 ? 'checked' : ''); . " ></td>";
echo "</tr>";
}
mysql_close($con);
?>
<?php
$sql = "SELECT * FROM registered ORDER BY datum";
$myData = mysql_query($sql, $con);
?>
<table width="1100" border="1">
<tr>
<th style="text-align:center; padding:0 10px">naam</th>
<th style="text-align:center; padding:0 10px">betaald?</b></th>
</tr>
<?php while ($record = mysql_fetch_array($myData)) { ?>
<tr>
<td><?php echo $record['naam'] ?></td>
<td>
<input type='checkbox' name='betaald' id='betaald' value='1'
<?php if($record['betaald'] == 1){ ?>
checked="checked"
<?php } ?>
/>
</td>
</tr>
<?php } ?>
</table>
Try this
<?php
$sql = "SELECT * FROM registered ORDER BY datum";
$myData= mysql_query($sql) ;
?>
<table width="1100" border="1">
<tr>
<th style="text-align:center; padding:0 10px">naam</th>
<th style="text-align:center; padding:0 10px">betaald</b></th>
</tr>
<?php
while($record = mysql_fetch_array($myData))
{
?>
<tr>
<td> <?php echo $record['naam'] ?></td>
<td> <input type='checkbox' name='betaald' id='betaald' value='1' <?php echo ($record['betaald']==1 ? 'checked' : '')?>> </td>
</tr>
<?php
}
mysql_close($con);
?>
mysql_query($sql) no need to define $con
Note: This extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0
<?php
$sql = "SELECT * FROM registered ORDER BY datum";
$myData=mysql_query($sql,$con) ;
?>
<table width="1100" border="1">
<tr>
<th style="text-align:center; padding:0 10px">naam</th>
<th style="text-align:center; padding:0 10px">betaald?</b></th>
</tr>
<?php
while($record = mysql_fetch_array($myData)) {
$betaald = $record['betaald'];
echo "<tr>";
echo "<td>" . $record['naam'] . "</td>";
echo "<td> <input type='checkbox' name='betaald' id='betaald' value='1' ";
echo $betaald==1 ? 'checked' : '';
echo " ></td>";
echo "</tr>";
}
mysql_close($con);
?>
You have assigned $betaald value outside the while loop so there is no value in $betaald.
I have included $betaald inside while loop.
You need several changes in your php and html code. First of all I've some confusion about your code. Let me clear those.
Point 1 [name='betaald']: If you have a name like this you will not get multiple selected value.
Point 2 [id='betaald']: ID must be unique in a page.
Point 3 [value='1']: How will you differ values if all have same value?
so your code should be something like this:
<?php
$sql = "SELECT * FROM registered ORDER BY datum";
$myData=mysql_query($sql,$con) ;
?>
<table width="1100" border="1">
<tr>
<th style="text-align:center; padding:0 10px">naam</th>
<th style="text-align:center; padding:0 10px">betaald?</b></th>
</tr>
<?php
$betaald = $_POST['betaald'];
$i = 1;
while($record = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>" . $record['naam'] . "</td>";
echo "<td> <input type='checkbox' name='betaald[]' id='betaald".$i."' value='".$record['betaald']."' ";
if ($record['betaald'] == $betaald)
{
echo "checked";
}
echo " ></td>";
echo "</tr>";
$i++;
}
mysql_close($con);
?>

Getting radio button value in while loop

I'm doing a simple examination program using PHP.
Here is my code for taking the quiz, it's still in testing so don't mind the design.
This is the session for the person who is taking the quiz (no problem here):
<?php
session_start ();
$_SESSION['username'];
?>
<div id="wrap">
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="myproject"; // Database name
$tbl_name="q_quiz"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['q_id'];
$sql="SELECT * FROM $tbl_name WHERE q_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td>
<table width="100%" frame="box" cellpadding="3" cellspacing="1" bgcolor="#CCFFCC">
<tr>
<td bgcolor="#CCFFCC"><h3>Quiz Info : </h3></td>
</tr>
<tr>
<td bgcolor="#CCFFCC"><strong>Quiz Name : </strong><?php echo $rows['q_name']; ?></td>
</tr>
</table>
</td>
</tr>
</table>
<font size="6">
<div style="font-weight: bold" id="quiz-time-left"></div>
<script type="text/javascript">
var max_time = <?php echo $rows['q_time'] ?>;
var c_seconds = 0;
var total_seconds =60*max_time;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
function init(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
setTimeout("CheckTime()",999);
}
function CheckTime(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds' ;
if(total_seconds <=0){
setTimeout('document.quiz.submit()',1);
} else {
total_seconds = total_seconds -1;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
setTimeout("CheckTime()",999);
}
}
init();
</script>
</font>
<form action="result.php" method="post">
<?php
$score = 0;
$tbl_name2="a_quiz"; // Switch to table "forum_answer"
$sql2="SELECT * FROM $tbl_name2 WHERE q_id='$id'";
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($result2)){
$q_question = $rows['q_question'];
?>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr style='overflow:hidden; word-wrap:break-word;'>
<input type="text" name="q_id" value="<?php echo $rows['q_id'];?>">
<td bgcolor="lightgreen"><strong>Question:</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen" style="max-width: 1000px;"><?php echo $rows['q_question']; ?></td>
</tr>
<tr>
<td bgcolor="lightgreen"><strong>A</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen"><input type="radio" name="<?php echo $q_question; ?>" value="a"><?php echo $rows['a'] ?></td>
</tr>
<tr>
<td width="18%" bgcolor="lightgreen"><strong>B</strong></td>
<td width="5%" bgcolor="lightgreen">:</td>
<td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $q_question; ?>" value="b"><?php echo $rows['b'] ?></td>
</tr>
<tr>
<td width="18%" bgcolor="lightgreen"><strong>C</strong></td>
<td width="5%" bgcolor="lightgreen">:</td>
<td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $q_question; ?>" value="c"><?php echo $rows['c'] ?></td>
</tr>
<tr>
<td bgcolor="lightgreen"><strong>D</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen"><input type="radio" name="<?php echo $q_question; ?>" value="d"><?php echo $rows['d'] ?></td>
</tr>
</table>
</td>
</tr>
</table>
<?php
}
$connection=mysql_connect('localhost', 'root','');
mysql_select_db('thesis');
$username= $_SESSION['username'];
$query6 = mysql_query("SELECT * FROM student WHERE username='$username'");
$row6 = mysql_fetch_assoc($query6);
$s_id = $row6['id'];
$name = $row6['name'];
$email = $row6['email'];
$position = $row6['position'];
?>
<input type="text" name="s_id" value="<?php echo $s_id ?>">
<input type="submit" name="submit" value="Submit Answer" class="btn">
</form>
</div>
I provided an image (I inputted two questions, this is the output of the code above):
http://i60.tinypic.com/2lu8doh.jpg
(2 is the quiz id, no problem on this, 12 near the submit button is the examiner id)
There's no problem in the code above.
My problem starts here. I am only getting the answer of the (examiner/person taking the quiz) of the question number 1 only and I already found my error.
This code below initiates when I click the submit button:
if(isset($_POST['submit']))
{
$con = mysql_connect ("localhost","root","");
if (!$con)
{
die("Error:".mysql_error());
}
mysql_select_db("myproject",$con);
}
I'm only getting the answer in the question number 1 of the examiner, because of this. It is not a loop so I'm only getting one answer even if theres 2-3 more answers.
$stud_id = $_POST['s_id'];
$quiz_id = $_POST['q_id'];
$score = 0;
$tbl_name2="a_quiz";
$sql2="SELECT * FROM $tbl_name2 WHERE q_id='$quiz_id'";
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($result2)){
$q_question=$rows['q_question'];
$question1 = $rows['answer'];
echo $q_question;
echo '<br>';
echo $question1;
echo '<br>';
if(isset($_POST[$q_question])){
$s_answer = $_POST[$q_question];
echo $s_answer;
if ($question1 == $s_answer){
$score = $score + 1;
}
}
}
echo $score;
So, for example, I got question 1 and question 2 and I selected the correct answer in number 1 I'm getting and $score++ but when my answer in question 2 is also correct is doesn't do $score++.
My problem here is: How can I get the value of radio button which I outputted them in while loop? Do I need to do a while loop for the answer too? How?
First, you are using each question as name attribute for your radio buttons, which is far from perfect. You might consider add a column to the table 'a_quiz' with a unique namespace for each question with no space in it (for instance question-one, question-two...) and call this column 'namespace'.
After this is done, you questions loop should look like this:
<?php
$score = 0;
$tbl_name2="a_quiz"; // Switch to table "forum_answer"
$sql2="SELECT * FROM $tbl_name2 WHERE q_id='$id'";
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($result2)){
$q_question = $rows['q_question'];
$namespace = $rows['namespace'];
?>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr style='overflow:hidden; word-wrap:break-word;'>
<input type="text" name="q_id" value="<?php echo $rows['q_id'];?>">
<td bgcolor="lightgreen"><strong>Question:</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen" style="max-width: 1000px;"><?php echo $rows['q_question']; ?></td>
</tr>
<tr>
<td bgcolor="lightgreen"><strong>A</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen"><input type="radio" name="<?php echo $namespace; ?>" value="a"><?php echo $rows['a'] ?></td>
</tr>
<tr>
<td width="18%" bgcolor="lightgreen"><strong>B</strong></td>
<td width="5%" bgcolor="lightgreen">:</td>
<td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $namespace; ?>" value="b"><?php echo $rows['b'] ?></td>
</tr>
<tr>
<td width="18%" bgcolor="lightgreen"><strong>C</strong></td>
<td width="5%" bgcolor="lightgreen">:</td>
<td width="77%" bgcolor="lightgreen"><input type="radio" name="<?php echo $namespace; ?>" value="c"><?php echo $rows['c'] ?></td>
</tr>
<tr>
<td bgcolor="lightgreen"><strong>D</strong></td>
<td bgcolor="lightgreen">:</td>
<td bgcolor="lightgreen"><input type="radio" name="<?php echo $namespace; ?>" value="d"><?php echo $rows['d'] ?></td>
</tr>
</table></td>
</tr>
<hr>
</table><br>
<?php
}
?>
Then, for the results part, use this code:
$stud_id = (int)$_POST['s_id'];
$quiz_id = (int)$_POST['q_id'];
$score = 0;
$tbl_name2 = "a_quiz";
$sql2 = "SELECT * FROM $tbl_name2 WHERE q_id='$quiz_id'";
$result2 = mysql_query($sql2);
$result = '';
while($rows=mysql_fetch_array($result2)){
$q_question = $rows['q_question'];
$question1 = $rows['answer'];
$namespace = $rows['namespace'];
$result .= $q_question;
$result .= <br>;
$result .= $question1;
$result .= <br>;
if(isset($_POST[$namespace])){
$s_answer = $_POST[$namespace];
$result .= $s_answer;
if ($question1 == $s_answer)
$score++;
}
}
$result .= $score;
echo $result;
}
?>

display all records of database table in div in php

here it showing all records from database but it shows all empty div first then below it shows all records .i want to show each record to be shown in single div in rows.
<div id="winnercontainer">
<h2 style="background-color:#9966FF; width:850px; height:30px; font:bold; font- size:22px; color:#FFFFFF; padding-left:20px;">Winners</h2>
<?php
include('pagination/ps_pagination.php');
$conn = mysql_connect('localhost','root','');
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('gunjanbid', $conn);
if(!$status) die("Failed to select database!");
$sql = 'SELECT * FROM winners';
$pager = new PS_Pagination($conn, $sql,10, 10);
//The paginate() function returns a mysql result set for the current page
$rs = $pager->paginate();
?>
<table>
<?php
while($row=mysql_fetch_array($rs)){
?>
<div id="winner">
<tr>
<td ><img src="memberpic/myphoto.jpg" width="150" height="150" alt="001" /></td>
<td > </td>
<td ><table >
<tr>
<td >Auction Item : <?php echo $row['Items']; ?></td>
</tr>
<tr>
<td>Rs. <?php echo $row['Rs']; ?></td>
</tr>
<tr>
<td>Winning Bid Value : Rs. <?php echo $row['WinningBidValue']; ?></td>
</tr>
<tr>
<td>MRP : Rs. <?php echo $row['MRP']; ?></td>
</tr>
<tr>
<td>Auction closed on : <?php echo $row['enddate']; ?></td>
</tr>
<tr>
<td>Winner : <?php echo $row['Winner']; ?></td>
</tr>
<tr>
<td>City: <?php echo $row['City']; ?></td>
</tr>
<tr>
<td >Delivery Status: <?php echo $row['DeliveryStatus']; ?></td>
</tr>
</table></td>
<td > </td>
<td id=save><font color=black>SAVED:</font> Rs.<?php echo $row['MRP']- $row['WinningBidValue']; ?></td>
<td > </td>
<td ><img src=productimage/1/1.1.jpg width="200" height="193" alt="001" /></td>
</tr>
</div>
<?php
}
?>
</table>
<?php
//Display the navigation
//echo $pager->renderFullNav();
echo '<div style="text-align:center">'.$pager->renderFullNav().'</div>';
?>
</div>
abd my stylesheet s are
#winnercontainer
{
width:870px;
height:auto;
background-color:#CCCCCC;
border:solid #9966FF;
border-radius:10px;
margin:auto;
clear:both;
margin:10px;
position:relative;
}
#winner
{
width:840px;
height:250px;
background-color: #999999;
border: solid #9966FF;
border-radius:10px;
margin:auto;
clear:both;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
margin-left: 15px;
position:relative;
}
i want all my records to be place in div means each div have one record to be display.plz help me ,i am new here.
I'll give you a start with some old code I have that reads a table from a MySQL database and prints it out on a screen. You can modify it as you see fit. It's a php file that resides on the server.
<?php
echo "<head><title>Read list</title></head>";
$host = "xxxxxxxxxxx";
$user = "xxxxxxxxxxx";
$password = "xxxxxxxxxxx";
$dbname = "xxxxxxxxxxx";
$cxn = mysqli_connect($host,$user,$password,$dbname);
if (mysqli_connect_errno()) {echo "No connection" . mysqli_connect_error();}
$query = " select * from list where uniqueid >= 0 ";
$result = mysqli_query($cxn, $query) or die ("could not query database");
echo "<table cellpadding=1px border=1>";
while ($row = mysqli_fetch_array($result))
{
$uniqueid = $row['uniqueid'];
$localid = $row['localid'];
$mdid = $row['mid'];
$type = $row['type'];
echo "<tr><td>".$uniqueid."</td><td>".$localid."</td><td>".$mdd."</td><td>".$type."</td></tr>";
}
echo "</table>";
?>

pagination - not working

I don't understand why my pagination is not working! It is displaying ok by default, but When click on 'Next' page or some page number nothing happens, not changing page at all. It's like not recognizing pagination.js or pagination_class.php.
I have another part of site where it is working fine, so not sure did I miss something obvious here?
In same folder have 4 files concerning this:myaccount_betinghistory.php, myaccount_bettinghistory_sub.php, pagination.js, pagination_class.php.
Here are all the codes of those files to have everything to look in, I hope you will be able to find bug there!
myaccount_bettinghistory.php:
<?php
error_reporting(E_ALL^E_NOTICE);
include('pagination_class.php');
?>
<script language="JavaScript" src="pagination.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
<?PHP
//Session start
#session_start();
//Path to root
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
//Require Init
$mode = array();
$mode[] = 'nohtml';
require_once $root . "/inc/php/init.php";
{
?>
<?PHP
}
?>
<?
$qry = "
SELECT timelive,bidprice,match_title,selection,winnings_with_startamount,odds,odds*10 AS gainedodds,username,
CASE
WHEN each_bid_recorded_Part2.result liKE '' AND each_bid_recorded_Part2.status LIKE 'E' THEN 'pending'
WHEN each_bid_recorded_Part2.result liKE '' AND each_bid_recorded_Part2.status NOT LIKE 'E' THEN 'active'
WHEN each_bid_recorded_Part2.result liKE each_bid_recorded_Part2.selection THEN 'WON'
WHEN each_bid_recorded_Part2.result NOT liKE each_bid_recorded_Part2.selection THEN 'LOST'
END AS result
FROM each_bid_recorded_Part2 WHERE each_bid_recorded_Part2.username LIKE '" . $_SESSION['username'] . "'
";
$qry .= " ORDER BY timelive DESC";
//for pagination
$starting=0;
$recpage = 4;//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';
}
else if ($status == 'active') {
return '#ffffcc';
}
return '';
}
?>
<div>
<div class="pageTop"><img src="images/icons/faqicon.png" width="37" height="37" align=absbottom> Betting History</div>
<tr>
<td> </td>
</tr>
</div>
<div>
<p style="font-size:12px; font-family: Verdana, Arial, Helvetica, sans-serif; color: #535252; text-align: left;">Check your bets with you as a last bidder.</p>
</div>
<table width="600" border="0" cellspacing="0" cellpadding="0" style="font-size:11px; color: #535252;">
<form name="form1" action="myaccount_bettinghistory.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="left" width="500px" padding-left="0px" style="background:#F9F9F9;">
<tr><TD colspan="0">
<div id="page_contents">
<table width="755px" cellspacing="0" cellpadding="5" align="center" frame="box" rules="none" style="padding-bottom:2px; margin-bottom:0px; 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: 40px;">
<td width="30%" align="center" td class="winheader"><div class="glowtext">Event Start Time</div></td></td>
<td width="10%" align="center" td class="winheader"><div class="glowtext">Bid Cost</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="5%" align="center" td class="winheader"><div class="glowtext">Final Result</div></td></td>
</tr>
<?if(mysql_num_rows($result)!=0){
$counter = $starting + 1;
while($data = mysql_fetch_array($result)) {?>
<tr class="initial" onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'">
<td align="center"><font color="#333"><? echo $data['timelive']; ?></TD>
<td align="center">€ <? echo $data['bidprice']; ?></TD>
<td align="left"><font color="#0070c0"><? 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>
<STYLE>
*{ margin-bottom:0; }
#pagination-flickr li{
margin:0px;
padding:0px;
float:left;
font-size:10px;
}
#pagination-flickr a{
float:left;
padding:5px 7px;
margin-right:5px;
border:solid 1px #4d7dc5;
text-decoration:none;
background:#FFFFFF;
color:#4d7dc5;
font-size:10px;
}
#pagination-flickr .previous-off,
#pagination-flickr .next-off {
border:solid 1px #DDDDDD;
cursor:default;
background:#FFFFFF;
border:solid 1px #BBBBBB;
color:#BBBBBB;
padding:4px 6px;
margin-right:5px;
font-size:10px;
}
#pagination-flickr .next a,
#pagination-flickr .previous a {
background:#FFFFFF;
border:solid 1px #BBBBBB;
color:#BBBBBB;
font-size:10px;
}
#pagination-flickr .active{
cursor:default;
background:#4d7dc5;
color:#FFFFFF;
padding:4px 6px;
margin-right:5px;
border:solid 1px #4d7dc5;
font-size:10px;
}
#pagination-flickr a:link,
#pagination-flickr a:visited {
padding:4px 6px;
margin-right:5px;
border:solid 1px #4d7dc5;
background:#FFFFFF;
color:#4d7dc5;
font-size:10px;
}
#pagination-flickr a:hover{
padding:4px 6px;
margin-right:5px;
border:solid 1px #4d7dc5;
background:#ffc04a;
color:#000;
font-size:10px;
}
body,table
{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
padding-bottom:5px;
empty-cells: show;
}
.glowtext
{
text-shadow: 0 0 20px white;
color:#333;
font-weight:bold;
}
</STYLE>
myaccount_bettinghistory_sub.php:
<?php
error_reporting(E_ALL^E_NOTICE);
include('pagination_class.php');
?>
<script language="JavaScript" src="pagination.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
<?PHP
//Session start
#session_start();
//Path to root
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
//Require Init
$mode = array();
$mode[] = 'nohtml';
require_once $root . "/inc/php/init.php";
{
?>
<?PHP
}
?>
<?
$qry = "
SELECT timelive,bidprice,match_title,selection,winnings_with_startamount,odds,odds*10 AS gainedodds,username,
CASE
WHEN each_bid_recorded_Part2.result liKE '' AND each_bid_recorded_Part2.status LIKE 'E' THEN 'pending'
WHEN each_bid_recorded_Part2.result liKE '' AND each_bid_recorded_Part2.status NOT LIKE 'E' THEN 'active'
WHEN each_bid_recorded_Part2.result liKE each_bid_recorded_Part2.selection THEN 'WON'
WHEN each_bid_recorded_Part2.result NOT liKE each_bid_recorded_Part2.selection THEN 'LOST'
END AS result
FROM each_bid_recorded_Part2 WHERE each_bid_recorded_Part2.username LIKE '" . $_SESSION['username'] . "'
";
$qry .= " ORDER BY timelive DESC";
//for pagination
if(isset($_GET['starting'])&& !isset($_REQUEST['submit'])){
$starting=$_GET['starting'];
}else{
$starting=0;
}
$recpage = 4;//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 '';
}
?>
<div>
<div class="pageTop"><img src="images/icons/faqicon.png" width="37" height="37" align=absbottom> Betting History</div>
<tr>
<td> </td>
</tr>
</div>
<div>
<p style="font-size:12px; font-family: Verdana, Arial, Helvetica, sans-serif; color: #535252; text-align: left;">Check your bets with you as a last bidder.</p>
</div>
<table width="600" border="0" cellspacing="0" cellpadding="0" style="font-size:11px; color: #535252;">
</div>
</div>
<table border="0" align="left" width="500px" padding-left="0px" style="background:#F9F9F9;">
<tr><TD colspan="0">
<div id="page_contents">
<table width="98%" cellspacing="0" cellpadding="5" align="center" frame="box" rules="none" style="padding-bottom:2px; margin-bottom:0px; 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: 40px;">
<td width="30%" align="center" td class="winheader"><div class="glowtext">Event Start Time</div></td></td>
<td width="10%" align="center" td class="winheader"><div class="glowtext">Bid Cost</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="5%" align="center" td class="winheader"><div class="glowtext">Final Result</div></td></td>
</tr>
<?if(mysql_num_rows($result)!=0){
$counter = $starting + 1;
while($data = mysql_fetch_array($result)) {?>
<tr class="initial" onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'">
<td align="center"><font color="#333"><? echo $data['timelive']; ?></TD>
<td align="center">€ <? echo $data['bidprice']; ?></TD>
<td align="left"><font color="#0070c0"><? 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>
</table>
pagination.js:
var xmlHttp
function pagination(page)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="myaccount_bettinghistory_sub.php";
url = url+"?starting="+page;
url = url+"&search_text="+document.form1.search_text.value;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("page_contents").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
pagination_class.php:
<?
/*
Developed by Reneesh T.K
reneeshtk#gmail.com
You can use it with out any worries...It is free for you..It will display the out put like:
First | Previous | 3 | 4 | 5 | 6 | 7| 8 | 9 | 10 | Next | Last
Page : 7 Of 10 . Total Records Found: 20
*/
class Pagination_class{
var $result;
var $anchors;
var $total;
function Pagination_class($qry,$starting,$recpage)
{
$rst = mysql_query($qry) or die(mysql_error());
$numrows = mysql_num_rows($rst);
$qry .= " limit $starting, $recpage";
$this->result = mysql_query($qry) or die(mysql_error());
$next = $starting+$recpage;
$var = ((intval($numrows/$recpage))-1)*$recpage;
$page_showing = intval($starting/$recpage)+1;
$total_page = ceil($numrows/$recpage);
if($numrows % $recpage != 0){
$last = ((intval($numrows/$recpage)))*$recpage;
}else{
$last = ((intval($numrows/$recpage))-1)*$recpage;
}
$previous = $starting-$recpage;
$anc = "<ul id='pagination-flickr'>";
if($previous < 0){
$anc .= "<li class='previous-off'><<</li>";
$anc .= "<li class='previous-off'><</li>";
}else{
$anc .= "<li class='next'><a href='javascript:pagination(0);'><< </a></li>";
$anc .= "<li class='next'><a href='javascript:pagination($previous);'>< </a></li>";
}
################If you dont want the numbers just comment this block###############
$norepeat = 4;//no of pages showing in the left and right side of the current page in the anchors
$j = 1;
$anch = "";
for($i=$page_showing; $i>1; $i--){
$fpreviousPage = $i-1;
$page = ceil($fpreviousPage*$recpage)-$recpage;
$anch = "<li><a href='javascript:pagination($page);'>$fpreviousPage </a></li>".$anch;
if($j == $norepeat) break;
$j++;
}
$anc .= $anch;
$anc .= "<li class='active'>".$page_showing."</li>";
$j = 1;
for($i=$page_showing; $i<$total_page; $i++){
$fnextPage = $i+1;
$page = ceil($fnextPage*$recpage)-$recpage;
$anc .= "<li><a href='javascript:pagination($page);'>$fnextPage</a></li>";
if($j==$norepeat) break;
$j++;
}
############################################################
if($next >= $numrows){
$anc .= "<li class='previous-off'>></li>";
$anc .= "<li class='previous-off'>>></li>";
}else{
$anc .= "<li class='next'><a href='javascript:pagination($next);'>> </a></li>";
$anc .= "<li class='next'><a href='javascript:pagination($last);'>>></a></li>";
}
$anc .= "</ul>";
$this->anchors = $anc;
$this->total = "Page : $page_showing of $total_page Total Records Found: $numrows";
}
}
?>
remove this
&& !isset($_REQUEST['submit'])
you are not using it !!!

Categories