im a real newbie when it comes to sql but im trying to pick up the pieces of my friends site... It all seems to be working fine apart from the left hand navigation to certain categories.
This is the site: http://tyresinwigan.co.uk/new/
The individual manufacturers should point to each manufacturer direct but they seem to be listing the results for all manufacturers.
Here is the code from the search.php:
<?php
require_once('const.php');
$link = dbConnect();
$manufacturer_id = 0;
$name = '';
if (isset($_GET['make']) && is_numeric($_GET['make'])) {
$manufacturer_id = (int) $_GET['make'];
}
$query = "SELECT manufacturer_name FROM manufacturer_tbl WHERE manufacturer_id = $manufacturer_id";
$result = false;
$result = #mysql_query($query, $link);
if (($result) && (#mysql_num_rows($result) > 0)) {
$row = #mysql_fetch_array($result, MYSQL_ASSOC);
$name = stripslashes($row['manufacturer_name']);
}
$query = "SELECT *,
v.vehicle_id AS vehicle_id_alias
FROM vehicle_tbl AS v
LEFT JOIN image_tbl AS i ON v.vehicle_id = i.vehicle_id
GROUP BY v.vehicle_id
HAVING v.manufacturer_id = $manufacturer_id";
$offers = false;
$offers = #mysql_query($query, $link);
$items = 0;
if ($offers) $items = mysql_num_rows($offers);
function nextOffer() {
global $offers;
global $items;
$items --;
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_alias'];
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 £'.$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 ' ';
}
}
?>
Change:
$query = "SELECT * FROM vehicle_tbl LEFT JOIN image_tbl ON vehicle_tbl.vehicle_id = image_tbl.vehicle_id
WHERE vehicle_tbl.manufacturer_id = $manufacturer_id
GROUP BY vehicle_tbl.vehicle_id";
to:
$query = "SELECT * FROM vehicle_tbl LEFT JOIN image_tbl ON vehicle_tbl.vehicle_id = image_tbl.vehicle_id
GROUP BY vehicle_tbl.vehicle_id
HAVING vehicle_tbl.manufacturer_id = $manufacturer_id";
You are looking at the wrong query. The problem isnt in the left menu, but the query on the resulting page. You need to look at that one, and make sure it is formatted properly.
Related
I have a table with a list of users and I want to create pagination for this table.
However, when I click page number 2 the page changes but not the contents of the list of users.
<?php
session_start();
require_once "connect.php";
//Database connect
$database = #new mysqli($host, $db_user, $db_password, $db_name);
//Limit users
$start = 0;
$limit = 3;
//Question to -> Database
$sql = ("SELECT * FROM users LIMIT $start, $limit");
$adds = $database->query($sql);
//Return records
$rows = mysqli_num_rows($adds);
echo "Rows found in database.. ".$rows."<br /><br />";
if(isset($_GET['id']))
{
$id=$_GET['id'];
$start=($id-1)*$limit;
}
else{
$id=1;
}
if ($rows >= 1)
{
//the beginning of the table
echo<<<END
<table width="1000" align="center" border="1" bordercolor="#d5d5d5" cellpadding="0" cellspacing="0">
<tr>
<td width="50" align="center" bgcolor="e5e5e5"></td>
<td width="50" align="center" bgcolor="e5e5e5">IDUsera</td>
<td width="20" align="center" bgcolor="e5e5e5">NazwaUsera</td>
<td width="20" align="center" bgcolor="e5e5e5">EmailUsera</td>
<td width="20" align="center" bgcolor="e5e5e5">DataRejestracji</td>
<td width="20" align="center" bgcolor="e5e5e5">Indetyfikator grupy</td>
</tr>
<tr>
END;
}
while($score = mysqli_fetch_assoc($adds)) {
$row_score_id = $score['id'];
$row_score_user = $score['user']." ";
$row_score_register = $score['dataRes']. " ";
//continued table
echo<<<END
<td width="50" align="center"><a class="button_red" href="delete_user.php?id=''">Delete</a></td>
<td width="50" align="center">$row_score_id</td>
<td width="100" align="center">$row_score_user</td>
<td width="100" align="center"></td>
<td width="100" align="center">$row_score_register</td>
<td width="100" align="center"></td>
</tr>
END;
}
$pagnSql = ("SELECT * FROM users");
$pagnBase = $database->query($pagnSql);
$pagnRows = mysqli_num_rows($pagnBase);
$total=ceil($pagnRows/$limit);
if($id>1)
{
//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
echo "<a href='?id=".($id-1)."' class='button'>PREVIOUS</a>";
}
if($id!=$total)
{
////Go to previous page to show next 10 items.
echo "<a href='?id=".($id+1)."' class='button'>NEXT</a>";
}
for($i=1;$i<=$total;$i++)
{
if($i==$id) { echo "<li class='current'>".$i."</li>"; }
else { echo "<li><a href='?id=".$i."'>".$i."</a></li>"; }
}
?>
You have to put the $start from GET
Change this
$start = 0;
To:
$start = $_GET[id];
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))
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 £'.$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
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)
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>';
}