rlb1
asked on
How do I echo the results of the SELECT statement?
I need to echo the results of the categories/subcategories below. I am having trouble with the correct select statement.
Thanks for any assistance you can give me!!
<?php
$sql = "SELECT DISTINCT category FROM ddcart_products";
$result = mysql_query($sql) or die('Error: '.mysql_error());
echo '<option value="">- Pick a Category -</option>';
while($data = mysql_fetch_assoc($result)
echo '<option value="'.$data['category']
$sql2 = "SELECT DISTINCT subcategory FROM ddcart_products
WHERE category = '{$data['category']}'";
$result2 = mysql_query($sql2) or die('Error: '.mysql_error());
if(mysql_num_rows($result2
/*echo '<optgroup>'*/;
while($data2 = mysql_fetch_assoc($result2
echo '<option value="'.$data2['subcatego
$data2['subcategory'].'</o
echo '</optgroup>';
}
}
echo $res;
?>
</select> <input type="submit" value="Go"></form></div>
<div class="clear" style="height: 1px;"></div>
</div>
<div class="clear" style="height: 1px;"></div>
<?php
//number of items per page
$numitems = getOptionValue('itemsperpa
if(isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 0;
}
//determine offset
$offset = $page*$numitems;
// NEED HELP GETTING RESULTS
$sql = "SELECT * FROM ddcart_products WHERE category = ".$res." LIMIT ".$offset.", ".$numitems;
$result = mysql_query($sql);
$i = 1;
while($data = mysql_fetch_assoc($result)
?>
<div class="productblock draggablediv" product="<?php echo $data['id'].'|'.$data['nam
<div class="pbimage"> <img width=150 src="<?php echo $data['photourl']; ?>"> <!--<?php echo displayProductImage($data[
<div class="pbinfo"><a href="productdetail.php?pr
<div class="pbtitle"><?php echo stripslashes($data['name']
<div class="pbprice"><?php echo "$".number_format($data['p
</div>
<?php
if(($i % 4) == 0) echo '<div class="clear"></div>';
$i++;
}
echo '<div class="clear"></div><div id="pagenums" style="text-align: center;">';
//total number of pages
$sql = "SELECT * FROM ddcart_products WHERE cat = '".$cat."'";
$result = mysql_query($sql);
$numpages = floor(mysql_num_rows($resu
for($i = 1; $i <= $numpages; $i++) {
if($page == $i) {
echo '<strong><span style="font-size: 12px; text-decoration: underline;"><a href="index.php?cat='.$cat
} else {
echo '<a href="index.php?cat='.$cat
}
}
echo '</div>';
?>
ASKER
hielo,
Thanks for your help!! I have found a solution to the Select drop down question, now I need to pull the items in each category and post them on my website using the code below..
<?php
//number of items per page
$numitems = getOptionValue('itemsperpa ge');
if(isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 0;
}
//determine offset
$offset = $page*$numitems;
// NEED HELP GETTING RESULTS
$sql = "SELECT * FROM ddcart_products WHERE category = ".$res." LIMIT ".$offset.", ".$numitems;
$result = mysql_query($sql);
$i = 1;
while($data = mysql_fetch_assoc($result) ) {
?>
<div class="productblock draggablediv" product="<?php echo $data['id'].'|'.$data['nam e'].'|'.$d ata['price ']; ?>">
<div class="pbimage"> <img width=150 src="<?php echo $data['photourl']; ?>"> <!--<?php echo displayProductImage($data[ 'id'], 'thumb'); ?>--></div>
<div class="pbinfo"><a href="productdetail.php?pr oduct=<?ph p echo $data['id']; ?>" class="detailslink">View Details</a></div>
<div class="pbtitle"><?php echo stripslashes($data['name'] ); ?></div>
<div class="pbprice"><?php echo "$".number_format($data['p rice'], 2, '.', ','); ?></div>
</div>
<?php
if(($i % 4) == 0) echo '<div class="clear"></div>';
$i++;
}
echo '<div class="clear"></div><div id="pagenums" style="text-align: center;">';
//total number of pages
$sql = "SELECT * FROM ddcart_products WHERE cat = '".$cat."'";
$result = mysql_query($sql);
$numpages = floor(mysql_num_rows($resu lt)/$numit ems);
for($i = 1; $i <= $numpages; $i++) {
if($page == $i) {
echo '<strong><span style="font-size: 12px; text-decoration: underline;"><a href="index.php?cat='.$cat .'&page='. $i.'">'.$i .'</a></st rong>';
} else {
echo '<a href="index.php?cat='.$cat .'&page='. $i.'">'.$i .'</a>';
}
}
echo '</div>';
?>
Thanks for your help!! I have found a solution to the Select drop down question, now I need to pull the items in each category and post them on my website using the code below..
<?php
//number of items per page
$numitems = getOptionValue('itemsperpa
if(isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 0;
}
//determine offset
$offset = $page*$numitems;
// NEED HELP GETTING RESULTS
$sql = "SELECT * FROM ddcart_products WHERE category = ".$res." LIMIT ".$offset.", ".$numitems;
$result = mysql_query($sql);
$i = 1;
while($data = mysql_fetch_assoc($result)
?>
<div class="productblock draggablediv" product="<?php echo $data['id'].'|'.$data['nam
<div class="pbimage"> <img width=150 src="<?php echo $data['photourl']; ?>"> <!--<?php echo displayProductImage($data[
<div class="pbinfo"><a href="productdetail.php?pr
<div class="pbtitle"><?php echo stripslashes($data['name']
<div class="pbprice"><?php echo "$".number_format($data['p
</div>
<?php
if(($i % 4) == 0) echo '<div class="clear"></div>';
$i++;
}
echo '<div class="clear"></div><div id="pagenums" style="text-align: center;">';
//total number of pages
$sql = "SELECT * FROM ddcart_products WHERE cat = '".$cat."'";
$result = mysql_query($sql);
$numpages = floor(mysql_num_rows($resu
for($i = 1; $i <= $numpages; $i++) {
if($page == $i) {
echo '<strong><span style="font-size: 12px; text-decoration: underline;"><a href="index.php?cat='.$cat
} else {
echo '<a href="index.php?cat='.$cat
}
}
echo '</div>';
?>
Well that was not useful feedback. Not clear what the problem is.
do you know if you are even getting any results? Try:
$sql = "SELECT * FROM ddcart_products WHERE category = ".$res." LIMIT ".$offset.", ".$numitems;
$result = mysql_query($sql) or die(mysql_error() );
echo "Categories Found: " . mysql_num_rows($result);
$res has no value, it is a leftover from an earlier attempt in the Related Solution (ID:22826957).
You probably want to list the products from the category selected in the dropdown? The below code will only work with categories, not with the sub-categories. You need to change the select dropdown to be able to list products within a sub-category.
if(isset($_POST['category'])) {
$category = $_POST['category'];
$sql = "SELECT * FROM ddcart_products WHERE category = ".$res." LIMIT ".$offset.", ".$numitems;
$result = mysql_query($sql);
$i = 1;
while($data = mysql_fetch_assoc($result)) {
echo
'<div class="productblock draggablediv">'.
'<div class="pbimage">'.
'<img width=150 src="'.$data['photourl'].'" alt="" />'.
'</div>'.
'<div class="pbinfo"><a href="productdetail.php?product='.$data['id'].
'" class="detailslink">View Details</a></div>'.
'<div class="pbtitle">'.stripslashes($data['name']).'</div>'.
'<div class="pbprice">$'.number_format($data['price'], 2, '.', ',').'</div>'.
'</div>';
if(($i % 4) == 0)
echo '<div class="clear"></div>';
$i++;
}
echo '<div class="clear"></div><div id="pagenums" style="text-align: center;">';
//total number of pages
$sql = "SELECT count(*) FROM ddcart_products WHERE category = '".$category."'";
$result = mysql_query($sql);
$numpages = ceil(mysql_num_rows($result)/$numitems);
for($i = 1; $i <= $numpages; $i++) {
if($page == $i) {
echo '<strong><span style="font-size: 12px; text-decoration: underline;">'.
'<a href="index.php?category='.$category.'&page='.$i.'">'.$i.'</a></strong>';
} else {
echo '<a href="index.php?category='.$category.'&page='.$i.'">'.$i.'</a>';
}
}
echo '</div>';
}
Sorry, line 3 should read:
$sql = "SELECT * FROM ddcart_products WHERE category = '".$category."' LIMIT ".$offset.", ".$numitems;
...and line 19 has wrong indentation, it is not part of the if in line 17/18.
ASKER
cxr, OK, I have a blank screen with no results. Line 19 looks OK to me, how should it read? Please clarify. Does EE have private messaging? Is there a way I can email you direct through EE?
Thanks!
Randy Bass
Thanks!
Randy Bass
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help!! I appreciate it.
Open in new window