Same results from Database - php

I need to add "Last member topics" in the members profile, so that it shows the last 5 topics that member wrote.
I used this code:
$last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
$result = mysql_num_rows($last_topic);
if ($result > 0){
$rslast = mysql_fetch_array($last_topic);
$topic_id = $rslast['TOPIC_ID'];
$topic_subject = $rslast['T_SUBJECT'];
$topic_mes = $rslast['T_MESSAGE'];
$t_ret = $rslast['T_ENUM'];
$i=0;
while ($i < $result){
echo '<tr>
<td colSpan="2" align="center" class="userdetails_data" width="100%">
<table border="0" width="100%">
<tr>
<td>
<font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
'.$topic_subject.'</a>
</td>
<td class="editedby" width="130">
<font face="Arial" style="font-size: 9pt"> Edited
:
'.$t_ret.'</font></td>
</tr>
</table>
</td>
</tr>
';
++$i;
}
}else {
echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
<span lang="ar-eg">
<font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
}
It works, but showing the same topic in the 5 rows??!!
any solutions?

its because you are assigning a $rslast one time and then using this variable in loop ,
try this
$last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
$result = mysql_num_rows($last_topic);
if ($result > 0){
while($rslast = mysql_fetch_array($last_topic)) {
$topic_id = $rslast['TOPIC_ID'];
$topic_subject = $rslast['T_SUBJECT'];
$topic_mes = $rslast['T_MESSAGE'];
$t_ret = $rslast['T_ENUM'];
echo '<tr>
<td colSpan="2" align="center" class="userdetails_data" width="100%">
<table border="0" width="100%">
<tr>
<td>
<font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
'.$topic_subject.'</a>
</td>
<td class="editedby" width="130">
<font face="Arial" style="font-size: 9pt"> Edited
:
'.$t_ret.'</font></td>
</tr>
</table>
</td>
</tr>
';
}
}else {
echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
<span lang="ar-eg">
<font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
}

You are getting same value because your $result var has total rows from query which in this case is 5 while you are saving the value to variable $rslast, instead use while loop so that you can loop over array values.
$last_topic = mysql_query("SELECT * FROM " .prefix. "TOPICS WHERE T_AUTHOR = '".$ProMemberID."' ORDER BY T_DATE DESC LIMIT 5") or die (mysql_error());
$result = mysql_num_rows($last_topic);
if ($result > 0){
while($rslast = mysql_fetch_array($last_topic,MYSQL_ASSOC)) {
//^-----while Loop added to loop through arrays, ^----associative array
$topic_id = $rslast['TOPIC_ID'];
$topic_subject = $rslast['T_SUBJECT'];
$topic_mes = $rslast['T_MESSAGE'];
$t_ret = $rslast['T_ENUM'];
echo '<tr>
<td colSpan="2" align="center" class="userdetails_data" width="100%">
<table border="0" width="100%">
<tr>
<td>
<font style="font-size: 9pt" face="Arial"><a href="index.php?mode=t&t='.$topic_id.'">
'.$topic_subject.'</a>
</td>
<td class="editedby" width="130">
<font face="Arial" style="font-size: 9pt"> Edited :'.$t_ret.'</font></td>
</tr>
</table>
</td>
</tr>';
}}else {
echo '<tr><td colSpan="2" align="center" class="userdetails_data" width="100%">
<span lang="ar-eg">
<font color="#FF0000" style="font-size: 9pt" face="Arial">This member has no topics yet! </font></span></td></tr>';
}

Related

How to add value to existing value using MySQL?

hi dear friends yesterday I asked about a teachers rating system but did not get any idea please help me. i am making a teacher rating system where i want to rate every teacher by making text boxes in front of the name of each teacher .the number that the user will add in text box for a specific teacher is to be added to a current rate of teacher.but the code is not working properly.when i add values through text boxes it adds the value from the last text box to all rows.for example for the first teacher i want to add 2 ,for the second i want to add 4 and for the 3rd teacher i want to rate 3. so instead of 2 and 3 all the rating of all teachers are incremented by 3.how to solve the problem?please help i am giving code of 2 pages m.php and n.php
m.php
<form action="n.php" method="post" enctype="multipart/form-data">
<table width="642" height="215" border="10" align="left" cellspacing="0" >
<tr>
<th class="style5">Teacher ID</th>
<th width="90" class="style5">Teacher Name</th>
<th width="127" class="style5">Teacher Registration</th>
<th width="135" class="style5">Teacher Qualification</th>
<th width="92" class="style5">Teacher Subject</th>
<th width="92" class="style5">Action</th>
</tr>
<?php
include 'conn.php';
$sql = "SELECT * FROM teacher ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_array()){
$id=$row['tid'];
?>
<tr>
<td height="50" align="center" class="style5"><?php echo $row['tid'];?></td>
<td align="center" class="style5"><?php echo $row['tname'];?></td>
<td align="center" class="style5"><?php echo $row['treg'];?></td>
<td align="center" class="style5"><?php echo $row['qualification'];?></td>
<td align="center" class="style5"><?php echo $row['subject'];?></td>
<td align="center"> <input type="text" name="rating">
</td>
</tr>
<?php
}
}else{
echo "<center><p><font size=10/> No Records</p></center>";
}
$conn->close();
?><tr><td colspan="6">
<input type="submit" name="submit" value="Enter"></td></tr>
</table>
</form>
n.php
<?php
mysql_connect("localhost","root","") or die ("couldnt connnect to server");
mysql_select_db("project") or die ("couldnt connnect to database");
include 'conn.php';
if(isset($_POST['submit']))
{
$sql = "SELECT * FROM teacher";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_array()){
$id=$row['tid'];
$newvalue=$_POST['rating'];
$sql_update="UPDATE teacher set hits = hits +$newvalue where tid=".$id."";
mysql_query($sql_update) or die(mysql_error());
header("location:m.php");
}
}
}
Like this ... give rating name array with id from table and update the record with the right value and id ... but never do this way for username and password case ...
<form action="n.php" method="post" enctype="multipart/form-data">
<table width="642" height="215" border="10" align="left" cellspacing="0" >
<tr>
<th class="style5">Teacher ID</th>
<th width="90" class="style5">Teacher Name</th>
<th width="127" class="style5">Teacher Registration</th>
<th width="135" class="style5">Teacher Qualification</th>
<th width="92" class="style5">Teacher Subject</th>
<th width="92" class="style5">Action</th>
</tr>
<?php
include 'conn.php';
$sql = "SELECT * FROM teacher ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_array()){
$id=$row['tid'];
?>
<tr>
<td height="50" align="center" class="style5"><?php echo $row['tid'];?></td>
<td align="center" class="style5"><?php echo $row['tname'];?></td>
<td align="center" class="style5"><?php echo $row['treg'];?></td>
<td align="center" class="style5"><?php echo $row['qualification'];?></td>
<td align="center" class="style5"><?php echo $row['subject'];?></td>
<td align="center"> <input type="text" name="rating[<?php echo $id; ?>]">
</td>
</tr>
<?php
}
}else{
echo "<center><p><font size=10/> No Records</p></center>";
}
$conn->close();
?><tr><td colspan="6">
<input type="submit" name="submit" value="Enter"></td></tr>
</table>
</form>
<?php
mysql_connect("localhost","root","") or die ("couldnt connnect to server");
mysql_select_db("project") or die ("couldnt connnect to database");
include 'conn.php';
if(isset($_POST['submit']))
{
$sql = "SELECT * FROM teacher";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_array()){
$id=$row['tid'];
$newvalue=$_POST['rating'];
if(isset($newvalue[$id])) {
$new_temp_value = $newvalue[$id];
$sql_update="UPDATE teacher set hits = hits +$new_temp_value where tid=".$id."";
mysql_query($sql_update) or die(mysql_error());
}
}
}
header("location:m.php");
}

php and mysql. view topics date

I can final exams assignment of lecturers to create a website forum.
and now there is a problem in my coding.
please fix.
when creating a topic / thread, and we opened the topic / threadnya, there is a date that tell us when the topic / thread made.
but in my coding, showing all existing date in the database.
<?php
session_start();
if($_SESSION['logged'] == true)
{
if(isset($_SESSION['username']))
{ echo $username=$_SESSION["username"];
}else
{
header('Location:login.php');
}
}
?>
<!DOCTYPE html>
<html>
<head>
<!==CSS>
<style>
#clock{
pointer-events: none
}
</style>
<!close css>
<img src="head1.jpg" alt="icon" width="100%" height="110">
<img src="line1.jpg" alt="icon" width="100%" height="20">
<title>Venray</title>
<body background="alienbackground.jpg">
<font face="comic sans MS">
<!==Table Login==>
<table border="0" style="width:99%" bgcolor="#424242" align="center">
<tr>
<table border="0" style="width:98%" bgcolor="#424242" align="center">
<tr>
<td> <img src="start.gif" alt="icon" width="26" height="26"> </td>
<td rowspan="2" align="right">
<table border="0" bgcolor="#2E2E2E">
<tr>
<td><b>
<?php
{
echo "<tr><td><b><font color=green> Welcome, ";
echo $username;
echo '<span>, [Log Out]</span></li>';
echo "</tr></td></b></font>";
}
?>
</b></td>
</tr>
<tr>
<td id=clock align="right"><b><iframe src="http://free.timeanddate.com/clock/i4eh41xm/n108/tlsg/fn7/fs12/tct/pct/ftb/tt0/tw1/tm1/th1" frameborder="0" width="182" height="20" allowTransparency="true"></iframe>
</b></td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top"><b><font size="4">Community</font></b></td>
</tr>
</table>
</tr>
<p style="color:#ff0000">
<!==Table please welcome==>
<table border="0" style="width:98%" bgcolor="#424242" align="center">
<tr>
<td align="right">
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
require("conn.php");
$con = mysql_connect($server,$user,$pass);
$db = mysql_select_db($db);
$sql = "select * from d_admin order by username";
//echo $sql;
$ambil_data = mysql_query($sql);
while($data = mysql_fetch_array($ambil_data))
$result = mysql_query("SELECT * FROM d_admin");
$rows = mysql_num_rows($result);
echo "Member " . $rows . " * ";
?>
Post 1 * Topics 1 </td>
</tr>
</table>
<br>
<table border="1" width="98%" height="6%" align="center" bgcolor=#424242 >
<tr>
<td align="left" ><img src="hometopic.gif" width="40" height="40"> </td>
</tr>
<tr>
<table border="1" width="98%" height="40" align="center" bgcolor="#86B404" bordercolor=green>
<tr>
<th width="70%" align="left"><img src="open.png" width="40" height="40">Posting Display</th>
<td align="right"><img src="replay.png" width="150" height="40"></td>
</tr>
</tr>
<tr>
<table border="1" width="98%" height="70" align="center" bgcolor=#424242>
***<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$con = mysql_connect($server,$user,$pass);
$db = mysql_select_db($db);
$sql = "select * from tabel_topik ";
$ambil_data = mysql_query($sql);
while($data = mysql_fetch_array($ambil_data))
{
echo '<tr><th align="left" valign="top" colspan="2" >'.$data['date'].'</th></tr>';
echo '</tr>';
}
?>***
<tr><th align="center" width="15%">Venray</th><td align="left" width="85%">title</td></tr>
<tr><th align="top" width="15%">join dates</th><td align="left" rowspan="2" valign="top">awd</td></tr>
<tr><th align="center" valign="top" width="15%" height="400">9 Post</th></tr>
<tr>
<td align="left">*</td>
<td align="right" width="85%"><img src="replay.png" width="150" height="40"></td></tr>
</table>
</tr>
</table>
</body>
</font>
</head>
</html>
Get forum specific datas by passing the ID or any similar field in the query for starters. Then if you are looking to format the way in which the date is displayed, you could use the php date function like this:
while($data = mysql_fetch_array($ambil_data))
{
echo '<tr><th align="left" valign="top" colspan="2" >'.date('jS M, Y',strtotime($data['date'])).'</th></tr>'; //format the date as required
echo '</tr>';
}

SQL Help - Newbie

I'm picking up the pieces for a friend. His website used to work and pull featured products from his database, but it doesn't seem to be working. Its just showing the Error!!
Any help would be appreciated.... I'm not really up to date with SQL.
Here's the code:
<?php
require_once('const.php');
$link = dbConnect();
$query = "SELECT *
FROM vehicle_tbl, manufacturer_tbl
LEFT JOIN image_tbl ON vehicle_tbl.vehicle_id = image_tbl.vehicle_id
WHERE vehicle_tbl.manufacturer_id = manufacturer_tbl.manufacturer_id AND
vehicle_tbl.vehicle_feature2 = '1'
GROUP BY vehicle_tbl.vehicle_id
ORDER BY RAND()
LIMIT 1";
$result = false;
$result = #mysql_query($query, $link);
$fmain = false;
if (($result) && (#mysql_num_rows($result) > 0)) {
$fmain = #mysql_fetch_array($result, MYSQL_ASSOC);
#mysql_free_result($result);
}
$query = "SELECT *
FROM vehicle_tbl, manufacturer_tbl
LEFT JOIN image_tbl ON vehicle_tbl.vehicle_id = image_tbl.vehicle_id
WHERE vehicle_tbl.manufacturer_id = manufacturer_tbl.manufacturer_id AND
vehicle_tbl.vehicle_feature1 = '1'
GROUP BY vehicle_tbl.vehicle_id
ORDER BY RAND()
LIMIT 6";
$offers = false;
$offers = #mysql_query($query, $link);
function nextOffer() {
global $offers;
if ($offers && ($row = mysql_fetch_array($offers))) {
if (! isset($row['image_name'])) { // no image
$image = 'images/noimagesml.jpg';
} else {
$image = 'images/vehicles/sml/'.stripslashes($row['image_name']);
}
$title = stripslashes($row['manufacturer_name']).' '.stripslashes($row['vehicle_model']);
$price = number_format((float) $row['vehicle_price_pcm'], 2);
$id = (int) $row['vehicle_id'];
echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="contenthead"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="11" height="40" align="left" valign="top"><img src="images/featre_left_hd.gif" width="11" height="23"></td>
<td width="100%" align="left" valign="middle" class="contenthead">'.$title.'</td>
<td width="11" height="40" align="right" valign="top"><img src="images/featre_rght_hd.gif" width="11" height="23"></td>
</tr>
</table></td>
</tr>
<tr>
<td class="contentpane"><table width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td align="center" valign="middle"><img src="'.$image.'" width="100" height="58" class="bordered" alt="'.$title.'"></td>
</tr>
<tr>
<td align="center" valign="top" class="princing">from just &pound'.$price.' pcm</td>
</tr>
<tr>
<td align="right" valign="middle"><img src="images/more_butt.gif" width="54" height="20" border="0"></td>
</tr>
</table></td>
</tr>
</table>';
} else {
echo 'Error!!';
}
}
?>
Display mysql errors like this:
mysql_query($query, $link) or die(__FILE__ . ' Line ' . __LINE__ . ': ' . mysql_error());
This should help you debug.
P.S. the mysql_* functions are deprecated - http://php.net/manual/en/mysqlinfo.api.choosing.php

how to make table sorting and searching by

I a newbie with this language and I do not know how to make it.
So in here I have script like this :
<?php
include_once "library/inc.sesadmin.php";
include_once "library/inc.library.php";
$row = 20;
$hal = isset($_GET['hal']) ? $_GET['hal'] : 0;
$pageSql = "SELECT * FROM kategori";
$pageQry = mysql_query($pageSql, $koneksidb) or die ("error paging: ".mysql_error());
$jml = mysql_num_rows($pageQry);
$max = ceil($jml/$row);
?>
<table width="700" border="0" cellpadding="2" cellspacing="1" class="table-border">
<tr>
<td colspan="2" align="right"><h1><b>DATA KATEGORI</b></h1></td>
</tr>
<tr>
<td colspan="2"><img src="images/btn_add_data2.png" height="25" border="0" /></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><table class="table-list" width="100%" border="0" cellspacing="1" cellpadding="2">
<tr>
<th width="32" align="center"><b>No</b></th>
<th width="93" align="center"><b>Kode</b></th>
<th width="444"><b>Nama Kategori </b></th>
<th width="93" align="center"><b>Jumlah Barang</b> </th>
<td width="47" align="center" bgcolor="#CCCCCC"><b>Ubah</b></td>
<td width="52" align="center" bgcolor="#CCCCCC"><b>Hapus</b></td>
</tr>
<?php
$kategoriSql = "SELECT kategori.*, (SELECT COUNT(*) FROM barang WHERE barang.kd_kategori=kategori.kd_kategori) As qty_barang
FROM kategori ORDER BY kd_kategori ASC LIMIT $hal, $row";
$kategoriQry = mysql_query($kategoriSql, $koneksidb) or die ("Query kategori salah : ".mysql_error());
$nomor = 0;
while ($kategoriRow = mysql_fetch_array($kategoriQry)) {
$nomor++;
$Kode = $kategoriRow['kd_kategori'];
?>
<tr>
<td align="center"><b><?php echo $nomor; ?></b></td>
<td align="center"><b><?php echo $kategoriRow['kd_kategori']; ?></b></td>
<td><?php echo $kategoriRow['nm_kategori']; ?></td>
<td align="center"><?php echo $kategoriRow['qty_barang']; ?></td>
<td align="center"><img src="images/btn_edit.png" width="20" height="20" border="0" /></td>
<td align="center"><img src="images/btn_delete.png" width="20" height="20" border="0" alt="Delete Data" /></td>
</tr>
<?php } ?>
</table></td>
</tr>
<tr>
<td><b>Jumlah Data :</b> <?php echo $jml; ?> </td>
<td align="right"><b>Halaman ke :</b>
<?php
for ($h = 1; $h <= $max; $h++) {
$list[$h] = $row * $h - $row;
echo " <a href='?page=Data-Kategori&hal=$list[$h]'>$h</a> ";
}
?>
</td>
</tr>
</table>
I want to make table sorting and search by at this table, but I do not know how to make it.
Can anyone help me for this simple project??
I really appreciate your big help in here..
I'm not going to give you the exact code because it's not how we do things in SO,
However I will share with you the main idea of how to accomplish it.
For each column add a link, for example:
<td><a href='?orderby=id'>ID</a></td>
<td><a href='?orderby=name'>Name</a></td>
When id and name are fields in the table (SQL & html).
Before the query you check if there's a LEGIT value for orderby ($_GET['orderby'])
and if so add it to the ORDER BY ... in the query.
Please notice that you're using mysql_* which is not safe (especially in this case),
consider using mysqli_* or PDO (you can google it)
EDIT
You can add &direction= to the columns' links to set the direction (DESC / ASC)

Pulling last years data from a table

Hello I'm trying to update this scholarship application box. However when the year changed to 2013 it only displays scholarship applicant info from 2013. I'd like it to display info from 2012. I tried messing around with the date but I cant seem to figure it out. Any help would be greatly appreciated!
<?php
$appYear = date("Y").'-'.(date("Y")+1);
$sql = 'select * from sApplication where studentID = "'.$database->iPrep($_SESSION['ID']).'" AND appYear = "'.$appYear.'" LIMIT 1';
$appID = Scholarship::iFindSQL($sql);
$total = count($appID);
if ($total > 0)
{
$app = array_shift($appID);
}
else
{
$app = 0;
}
?>
<li id="item-2">
<div id="appStatus">
<h3>Application Status</h3>
<blockquote>
<?php if ($app->submitted == ('0000-00-00') || !isset($app->submitted)) { ?>
<table style="border:1px solid #000;" width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td width="50%"><strong>Scholarship<br /> 2013-2014</strong></td>
<td width="50" align="right"> <a style="font-size:16px;" href="welcome.php? app=Scholar">Apply Now</a></td>
</tr>
<tr>
<td><strong>Date Submitted</strong></td>
<td align="right"> </td>
</tr>
<tr>
<td><strong>References</strong></td>
<td align="right"> </td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td> </td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>
<tr>
<td><strong>Scholarship 2012-2013</strong></td>
<td> </td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td> </td>
</tr>
</table>
<?php } else { ?>
<table style="border:1px solid #000;" width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td width="90%"><strong>Scholarship 2013-2014</strong></td>
<td width="10%" align="right"> </td>
</tr>
<tr>
<td><strong>Date Submitted</strong></td>
<td align="right"><?=dbOutDate($app->submitted)?></td>
</tr>
<tr>
<td><strong>References</strong> </td>
<td align="right"></td>
</tr>
<tr>
<td colspan="2">
<?php
$refs = Reference::iFindSQL("Select * from reference where appID = '".$app->ID."'");?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php foreach($refs as $ref) { ?>
<tr> <td> <small><?php if($ref->rType == 'Academic Reference'){ echo 'Academic/Artistic/Professional'; } else { echo 'Community Service'; } ?></small></td> <td align="right"><?=$ref->status?></td></tr>
<?php } ?>
</table>
</td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td align="right">
<?php
if ($app->complete == 'Approved') { echo 'Approved'; }
if ($app->complete == 'Declined') { echo 'Declined'; }
if ($app->complete == 'Pending') { echo 'Pending'; }
if ($app->complete == 'Incomplete') { echo 'Incomplete'; }
Remove the WHERE clause that limits the year. Use ORDER BY to sort by year, descending.
$sql = 'select * from sApplication
where studentID = "'.$database->iPrep($_SESSION['ID']).
'" ORDER BY appYear DESC LIMIT 1';

Categories