Jump to content

Just a quick question! help for a noob!


jbrill

Recommended Posts

Hey im just in the middle of finalizing a site with a product database.

The "Admin" has the ability to add, remove modify products etc.

 

When adding products the also have the opportunity to check off the "featured product?" checkbox.

However i would only like this box to be clickable if there are 2 or less "featured procducts" per category-subcategory.

 

how would i go about doing this.

 

Here is part of the code that shows the the checkbox and the current PHP:

 

<td align=right>
	<font class=input>Featured?:
</td>
<td>
	<input type="Checkbox" name="featured" value="1" <?if($prod['featured']==1){echo "checked";}?>><font class=input>Featured<br>
</td>

 

Thanks in Advance!

 

 

Link to comment
Share on other sites

You would have to have a column that would correspond to featured being either on or off ( 1 of 0 ) respectively. Then when you loop through the database to pull out the products just say whether or not it is featured, then to change that just update the column/row to be 1 or 0.

Link to comment
Share on other sites

I wa sthinking somethign like

query mysql num row

 

from table products where category AND subcat

 

then...

 

if that is 2 or less display: able to be checked

if it is more than 2 disable box

 

...just dint know how to put that in code or if it will work?

Link to comment
Share on other sites

Yes, thats exactly the way to go:

 

<?php
$sql = "SELECT count(*) FROM `yourtable` WHERE `category`='$current_category` AND `subcategory`='$current_subcat' AND `featured`='1'";
$result = mysql_query($sql) or die(mysql_error());
$num = mysql_result($result,0,"count(*)");
if($num <= 2){
//can add a featured product
}else{
//cant add a featured product
}
?>

Link to comment
Share on other sites

yes i think mysql_num_rows is faster sorry mate?

 

<?php
$sql = "SELECT count(*) FROM `yourtable` WHERE `category`='$current_category` AND `subcategory`='$current_subcat' AND `featured`='1'";

$result = mysql_query($sql) or die("mysql_error()");

if(mysql_num_rows($result)<= 2){

//can add a featured product

}else{

//cant add a featured product
}
?>

Link to comment
Share on other sites

Hey, sorry to be a pain, one last question.

 

First of all thanks for the num rows code, looks like that should do the trick!!

however i have one problem here:

 

category`='$current_category` AND `subcategory`='$current_subcat'

 

The problem is that i do not know how to get those variables since those (the category and sub category) are chosen on the same page therefor i do not know how to pick the category or subcategory....

 

I just cant figure out once the drop down for category and subcategory are selected (these drop downs are located on the same page as the featured check box), how to get that to correspond with the featured check box and work appropriately

 

Here is my whole code for the page "add product" hopefully this makes my question a bit more clear:

<?php
include 'admin_header.php';

// this page loads new product information. Action at top, form at bottom


// this part validates whether the user is logged in as a administrator or not
if($_SESSION['type'] == "admin")
{











// execute the real stuff if the login is valid

include 'admin_prodmenu.php';
if($_POST['submit'])
	 {

	// codes to execute if something has been submitted already - this part loads it up and then takes you back to the whole list.




	// validations for valid entries

		if($_POST['publish']==1)
		{$publish=1;}
		else{$publish=0;}

		$prod_number = htmlspecialchars($_POST['prod_number']);           
		$manufacturer = htmlspecialchars($_POST['manufacturer']);     
		$name = htmlspecialchars($_POST['name']);      
		$category = htmlspecialchars($_POST['cat']);            

		$codequery = "SELECT subc FROM subcategory WHERE subcat='".$_POST['Subcat']."'";
		$code = mysql_query($codequery);
		$subc = mysql_fetch_array($code);

		$subcat = $subc['subc'];             
		             
		$color = $_POST['color'];              
		$size = $_POST['size'];             
            
		$description = $_POST['description'];          
		$origin = $_POST['origin'];
		$price = $_POST['price'];

		$photo = $_POST['photo'];           
		$quantity = $_POST['quantity'];           
		$keywords = $_POST['keywords'];           


		         




		if(!empty($_FILES['picture']))
		{// if there was a new file selected

		// section to process the photo
		$photovar = "../product_images/".$_FILES['picture']['name'];


		move_uploaded_file($_FILES['picture']['tmp_name'], $photovar);
		$photo = "product_images/".$_FILES['picture']['name'];

		}





		$insert = "INSERT INTO products 
		(date_mod,date_add,prod_number,manufacturer,name,cat,subcat,color,size,description,origin,price,photo,quantity,keywords,featured,stock,publish)
	VALUES ('".$currentdate."','".$currentdate."','".$prod_number."','".$manufacturer."','".$name."','".$cat."','".$subcat."','".$color."','".$size."','".$description."','".$origin."','".$price."','".$photo."','".$quantity."','".$keywords."','".$featured."','".$stock."','".$publish."')";





		mysql_query($insert);

		$success = mysql_affected_rows();

		$lastid = mysql_insert_id();

	loadimage("whmis","whmis",$lastid);


		if($success == 1)
		{echo "Actions Successful.";
		echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3; URL=admin_findprod.php\">";}
		else{die('Did not Load : '.mysql_error());}

		}


else{
// codes to run for the form itself



?>
<table border="0" cellspacing="0" cellpadding="0" bgcolor="white">
<tr>
<td class="MainBody1">

<form method="post" enctype="multipart/form-data" action="<?echo $PHP_SELF?>" name="categories">
<input type="Hidden" name="submit" value="1">

<table align=center border=0>
<tr>
<td align=center colspan=2>
<font class=section>
Add a New Product
</font>

</td>
</tr>


<tr>
<td align="right" width="140">
	<font class=input>Product Number: 
</td>
<td>
	<input type=text size=20 name=prod_number maxlength=255 value="<? echo $prod['prod_number']?>">
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Manufacturer:
</td>
<td>
	<input type=text size=30 name=manufacturer maxlength=255 value="<? echo $prod['manufacturer']?>">
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Name:
</td>
<td>
	<input type=text size=30 name=name maxlength=255 value="<? echo $prod['name']?>">
</td>
</tr>


<tr>
<td align=right>
	<font class=input>Category:
</td>
<td>
	<select name="cat" onChange="populateSubcat(document.categories,document.categories.cat.options[document.categories.cat.selectedIndex].value)">
<option selected value=''>Select Category</option>
<?

$catsql = "SELECT DISTINCT cat, category FROM category";
$cats = mysql_query($catsql);
$category = mysql_fetch_array($cats);

do

{
echo "<option value='".$category["cat"]."'>".$category["category"]."</option>";


} while ($category = mysql_fetch_array($cats));



?>



</td>
</tr>

<tr>
<td align=right>
	<font class=input>Sub Category:
</td>
<td>
	<select name="Subcat" onChange="populateUSstate(document.categories,document.categories.Subcat.options[document.categories.Subcat.selectedIndex].text)">
<option value=''>Select Category First</option>
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Color:
</td>
<td>
	<textarea rows="5" cols="50" name=color value="<? echo $prod['color']?>"></textarea>
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Size:
</td>
<td>
	<textarea rows="5" cols="50" name=size value="<? echo $prod['size']?>"></textarea>
</td>
</tr>


<tr>
<td align=right>
	<font class=input>Brief Description:
</td>
<td>
	<textarea name=description rows="5" cols="50"><? echo $prod['description']?></textarea>
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Country of Origin:
</td>
<td>
	<textarea name=origin rows="5" cols="50"><? echo $prod['origin']?></textarea>
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Price:
</td>
<td>
	<textarea name=price rows="5" cols="50"><? echo $prod['price']?></textarea>
</td>
</tr>
<tr>
					<td colspan="2" align=center>
							<a href="#" onclick="document.getElementById('extphotos').style.display=''">Show Existing Photos to choose from.</a></td>
				</tr>
				<tr>
					<td colspan="2" align=center><?include 'admin_photos.php';?></td>
</tr>
<tr>
<td align=right>
	<font class=input>Photo Link:
</td>
<td>
	<input type=text size=30 name=photo maxlength=255 value="<? echo $prod['photo']?>">
</td>
</tr>
<tr>
<td align=right>
	<font class=input>Add a Photo:
</td>
<td>
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
	<input type="File" name="picture">
</td>
</tr>
<tr>
<td align=right>
	<font class=input>Priced Per:
</td>
<td>

	<input type=text size=30 name=quantity maxlength=255 value="<? echo $prod['quantity'];?>">
</td>
</tr>
<tr>
<td align=right>
	<font class=input>Product Keywords:
</td>
<td>
	<textarea rows="5" cols="50" name=keywords value="<? echo $prod['keywords']?>"></textarea>
</td>
</tr>
<tr>
<td align=right>
	<font class=input>Featured?:
</td>
<td>
	<input type="Checkbox" name="featured" value="1" <?if($prod['featured']==1){echo "checked";}?>><font class=input>Featured<br>
</td>
</tr>
<tr>
<td align=right>
	<font class=input>Stock?:
</td>
<td>
	<input type="Checkbox" checked name="stock" value="1" <?if($prod['stock']==1){echo "checked";}?>><font class=input>Stock<br>
</td>
</tr>


<tr>
<td align=right>
	<font class=input>Publish?:
</td>
<td>
	<input type="Checkbox" checked name="publish" value="1" <?if($prod['publish']==1){echo "checked";}?>><font class=input>Publish<br>
</td>
</tr>


<tr>
<td colspan=2 align=center>
<input type=submit value="Add This Product">
</td>
</tr>
</table>

</form>


<?



}



}

else {
echo "You do not have authority to modify this page, either because this is not your account, or you are not an administrator.";

}
?>
			<br>
		</td>
	</tr>
</table>
<br>
<?
require 'admin_footer.php';
?>

 

thanks again!

Link to comment
Share on other sites

this is the whole page for "Add product"

<?php
include 'admin_header.php';

// this page loads new product information. Action at top, form at bottom


// this part validates whether the user is logged in as a administrator or not
if($_SESSION['type'] == "admin")
{











// execute the real stuff if the login is valid

include 'admin_prodmenu.php';
if($_POST['submit'])
	 {

	// codes to execute if something has been submitted already - this part loads it up and then takes you back to the whole list.




	// validations for valid entries

		if($_POST['publish']==1)
		{$publish=1;}
		else{$publish=0;}

		$prod_number = htmlspecialchars($_POST['prod_number']);           
		$manufacturer = htmlspecialchars($_POST['manufacturer']);     
		$name = htmlspecialchars($_POST['name']);      
		$category = htmlspecialchars($_POST['cat']);            

		$codequery = "SELECT subc FROM subcategory WHERE subcat='".$_POST['Subcat']."'";
		$code = mysql_query($codequery);
		$subc = mysql_fetch_array($code);

		$subcat = $subc['subc'];             
		             
		$color = $_POST['color'];              
		$size = $_POST['size'];             
            
		$description = $_POST['description'];          
		$origin = $_POST['origin'];
		$price = $_POST['price'];

		$photo = $_POST['photo'];           
		$quantity = $_POST['quantity'];           
		$keywords = $_POST['keywords'];           


		         




		if(!empty($_FILES['picture']))
		{// if there was a new file selected

		// section to process the photo
		$photovar = "../product_images/".$_FILES['picture']['name'];


		move_uploaded_file($_FILES['picture']['tmp_name'], $photovar);
		$photo = "product_images/".$_FILES['picture']['name'];

		}





		$insert = "INSERT INTO products 
		(date_mod,date_add,prod_number,manufacturer,name,cat,subcat,color,size,description,origin,price,photo,quantity,keywords,featured,stock,publish)
	VALUES ('".$currentdate."','".$currentdate."','".$prod_number."','".$manufacturer."','".$name."','".$cat."','".$subcat."','".$color."','".$size."','".$description."','".$origin."','".$price."','".$photo."','".$quantity."','".$keywords."','".$featured."','".$stock."','".$publish."')";





		mysql_query($insert);

		$success = mysql_affected_rows();

		$lastid = mysql_insert_id();

	loadimage("whmis","whmis",$lastid);


		if($success == 1)
		{echo "Actions Successful.";
		echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3; URL=admin_findprod.php\">";}
		else{die('Did not Load : '.mysql_error());}

		}


else{
// codes to run for the form itself



?>
<table border="0" cellspacing="0" cellpadding="0" bgcolor="white">
<tr>
<td class="MainBody1">

<form method="post" enctype="multipart/form-data" action="<?echo $PHP_SELF?>" name="categories">
<input type="Hidden" name="submit" value="1">

<table align=center border=0>
<tr>
<td align=center colspan=2>
<font class=section>
Add a New Product
</font>

</td>
</tr>


<tr>
<td align="right" width="140">
	<font class=input>Product Number: 
</td>
<td>
	<input type=text size=20 name=prod_number maxlength=255 value="<? echo $prod['prod_number']?>">
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Manufacturer:
</td>
<td>
	<input type=text size=30 name=manufacturer maxlength=255 value="<? echo $prod['manufacturer']?>">
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Name:
</td>
<td>
	<input type=text size=30 name=name maxlength=255 value="<? echo $prod['name']?>">
</td>
</tr>


<tr>
<td align=right>
	<font class=input>Category:
</td>
<td>
	<select name="cat" onChange="populateSubcat(document.categories,document.categories.cat.options[document.categories.cat.selectedIndex].value)">
<option selected value=''>Select Category</option>
<?

$catsql = "SELECT DISTINCT cat, category FROM category";
$cats = mysql_query($catsql);
$category = mysql_fetch_array($cats);

do

{
echo "<option value='".$category["cat"]."'>".$category["category"]."</option>";


} while ($category = mysql_fetch_array($cats));



?>



</td>
</tr>

<tr>
<td align=right>
	<font class=input>Sub Category:
</td>
<td>
	<select name="Subcat" onChange="populateUSstate(document.categories,document.categories.Subcat.options[document.categories.Subcat.selectedIndex].text)">
<option value=''>Select Category First</option>
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Color:
</td>
<td>
	<textarea rows="5" cols="50" name=color value="<? echo $prod['color']?>"></textarea>
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Size:
</td>
<td>
	<textarea rows="5" cols="50" name=size value="<? echo $prod['size']?>"></textarea>
</td>
</tr>


<tr>
<td align=right>
	<font class=input>Brief Description:
</td>
<td>
	<textarea name=description rows="5" cols="50"><? echo $prod['description']?></textarea>
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Country of Origin:
</td>
<td>
	<textarea name=origin rows="5" cols="50"><? echo $prod['origin']?></textarea>
</td>
</tr>

<tr>
<td align=right>
	<font class=input>Price:
</td>
<td>
	<textarea name=price rows="5" cols="50"><? echo $prod['price']?></textarea>
</td>
</tr>
<tr>
					<td colspan="2" align=center>
							<a href="#" onclick="document.getElementById('extphotos').style.display=''">Show Existing Photos to choose from.</a></td>
				</tr>
				<tr>
					<td colspan="2" align=center><?include 'admin_photos.php';?></td>
</tr>
<tr>
<td align=right>
	<font class=input>Photo Link:
</td>
<td>
	<input type=text size=30 name=photo maxlength=255 value="<? echo $prod['photo']?>">
</td>
</tr>
<tr>
<td align=right>
	<font class=input>Add a Photo:
</td>
<td>
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
	<input type="File" name="picture">
</td>
</tr>
<tr>
<td align=right>
	<font class=input>Priced Per:
</td>
<td>

	<input type=text size=30 name=quantity maxlength=255 value="<? echo $prod['quantity'];?>">
</td>
</tr>
<tr>
<td align=right>
	<font class=input>Product Keywords:
</td>
<td>
	<textarea rows="5" cols="50" name=keywords value="<? echo $prod['keywords']?>"></textarea>
</td>
</tr>
<tr>
<td align=right>
	<font class=input>Featured?:
</td>
<td>
	<input type="Checkbox" name="featured" value="1" <?if($prod['featured']==1){echo "checked";}?>><font class=input>Featured<br>
</td>
</tr>
<tr>
<td align=right>
	<font class=input>Stock?:
</td>
<td>
	<input type="Checkbox" checked name="stock" value="1" <?if($prod['stock']==1){echo "checked";}?>><font class=input>Stock<br>
</td>
</tr>


<tr>
<td align=right>
	<font class=input>Publish?:
</td>
<td>
	<input type="Checkbox" checked name="publish" value="1" <?if($prod['publish']==1){echo "checked";}?>><font class=input>Publish<br>
</td>
</tr>


<tr>
<td colspan=2 align=center>
<input type=submit value="Add This Product">
</td>
</tr>
</table>

</form>


<?



}



}

else {
echo "You do not have authority to modify this page, either because this is not your account, or you are not an administrator.";

}
?>
			<br>
		</td>
	</tr>
</table>
<br>
<?
require 'admin_footer.php';
?>

 

do you need anything else?

Link to comment
Share on other sites

he gave you a way of you been the adming when you log in with a session so when you input your username and password you can add a admin level such as

<?php

if(username==username && password==$password){

$_SESSION['admin']='yes';
}
?>

so now that session tells your the admin and to dis play anything that is for your self just use your admin power such as

<?php
if($_SESSION['admin']){

// only the admin can see this.
?>
}

Link to comment
Share on other sites

your question was, a way to give you admin powers right?

 

that is the easiest way to do it..

 

after you have the admin session

 

you can just code out what ever you want yourself to do..

 

so when you visit a page you are able to remove or add anything you want.

Link to comment
Share on other sites

haha no, sorry my question was confusing.

 

My admin powers are working fine. the problem is, once i use the drop down to find the category, i am then able to select a corresponding sub category under that category.

 

the next thing i would like it to do, after the user selects a sub category is check to see if there is 2 or more "featured products" in that subcategory. if there is more than 2 disable the check box for "featured products" if there is less than 2 in that category, allow the check box to be checked.

 

 

Hope that clears it up a bit! sorry for the miss communication!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.