Jump to content

Can I dynamically create a form from info in the database?


bullbreed

Recommended Posts

HI everyone.

 

I am learning PHP by developing an ecommerce system.

What I am trying to do is  create an admin facility so the admin can add a product to the database.

I have most of this done but i'm having a problem with the colour options of the products.

 

I want the admin to be able to either select an existing colour from colours existing in the database#and

allow them to create a colour if one doesn't exist.

 

But I want to use images to represent the colours and i dont want to use javascript. So heres what I want to do.

 

When the admin adds a product (lets say a T-Shirt) it may be available in different colours so I want to echo 2 small colour images from the database. black.jpg and white.jpg. But I want to also echo out a checkbox benieth the colour image that represents that colour. So that when the admin selects the colours that a particukar t-shirt is available in he simply checks the check boxes relevant to the colours available. When he submits the form to add the product it stores the selected colour check boxes in the database relative to that particular t-shirt.

 

However, if the colour doesnt exist in the database (lets say red) I would like the admin to be able to create a colour image (red.jpg). Now they could do this by either creating an image then uploading it or enter the hex colour code in to a form field and ley phpo gd dcreate the image, save it to the database and call it up to the screen with a checkbox.

 

I think it will look good and make it simple for the user to add colour options even though it might be complicated to do in PHP.  ;)

 

But thats why i'm here, lerning php  :D

 

Could you point me in the right direction to get this started please

Link to comment
Share on other sites

It's a bad idea to let the user create the colour images or insert hex colour codes as they won't know how to create such an image nor do they know what HEX means. Keep It Simple!

 

Not everybody is a dope man :)

 

It is also not good to limit the systems ability to accommodate all products. I would personally add a color picker using javascript and build the image with gd, but like most people you have a caveat of "I don't want to use javascript" with no real explanation of why.

 

Good luck!

 

 

Link to comment
Share on other sites

Yes you can.... but how is the important part. Not sure what your specific question was there. But good references for how to do things are using this site, and w3schools http://www.w3schools.com/php/ as well as referring back to the php.net site. Then when you can't get a specific function to work as intended or don't understand ask and many here will answer.

 

And an alternative to javascript is using a series of css, html, and php code. Since javascript is client and nice for dynamic here and now displays.  php can do it but  you have to remember it is a server side language. So you will have to use forms to submit the information or something along those methods and lose the here and now displays. But if for an admin page see no reason you would need to have to use javascript just give them a list from a database/txtfile/whatever and display a selection grid.... and could easily be updated if they want to add a color. Easily done in php.

Link to comment
Share on other sites

I suppose I am being restrictive by not wanting to use javascript but the reason I didn't want to is because if I were to develop this as a working project for a client who didn't have javascript turned on they would not be able to add a colour option. Saying that I don't have statistics showing how many people actually turn javascript off.

 

If it were all PHP then we wouldn't have any issues.

 

I could use javascript to allow the user to select a colour and that colour hex value could be inserted in to a text field then read by the GD script to make the image once submitted.

 

But once these are in the database I want the admin page add-product.php to echo out the colour images with a check box next to it so the user simply clicks the checkbox to associate a colour for a product.

 

This i'm stuck on. Dynamically generating checkbox fields associated with a colour.

 

 

Link to comment
Share on other sites

Yeah, I personally do not know anybody who turns javascript off anymore. That being said, it should degrade gracefully as they say. If the javascript colour picker does not function because javascript is disabled, they could still type the hex value (most everybody has a photo/drawing program that will allow them to select the color and see the hex).

Link to comment
Share on other sites

But once these are in the database I want the admin page add-product.php to echo out the colour images with a check box next to it so the user simply clicks the checkbox to associate a colour for a product.

 

This i'm stuck on. Dynamically generating checkbox fields associated with a colour.

 

Yeah that's a good setup btw....

To answer that question if you have a database with the colors in it (or even a text file with this, not the recommended route but can be done too). Then run mysql queries to basically dump all the colors into an array if using a db. Then when displaying the checkbox for the colors list, or using javascript... etc... etc... run through a foreach or another type of loop to display all the colors how you want them out from the array that you stored all the colors in.

Link to comment
Share on other sites

Ok. I think i'll use Javascript and GD to create the images (well i'll try anyway lol)

 

But could you please give me a clue where to start echoing out the for each loop in a checkbox.

 

Heres what I came up with but haven't been doing php for that long.

 

Also could someone explain how I set up the database with test data because I know I have to serialize the data from the checkboxes but how can I create serialized data just to test the array works?

 

Heres what I initially came up with the echo the array in a checkbox but have no data to check it yet (idiot, i know)

 

What do you think? Would this work?

$sql = "SELECT `colours` FROM `products` WHERE `prod_name` = '$prodname' ";
			$query = mysql_query($sql) or die(mysql_error());

			$colours = array();

			while ($row = mysql_fetch_array($query)){
					$colours[] = $row[0];
					$colours = unserialize ($data['colours']);
			}
			$colours = array_unique($colours);
			?>
          <label>
          <input type="checkbox" name="prod_colours" id="prod_colours" value="<?php
				foreach($colours as $colour){
				echo '<option value="'.$colour.'">'.$colour.'</option>';
				}
			?>">

 

Link to comment
Share on other sites

Echo something in the manner of this, similiar to what you have. Looks like it may work with modifications for it to print out right.

<?php
//get my data
?>
<form>
<?php
loop here{
  echo $colour.": ";
  echo ('<input type=option value="'.$colour.'">
   <br> ');
///below example html code will look like
//I have a bike: <input type="checkbox" name="vehicle" value="Bike" />
//<br />
} ///end of loop here
?>
</form> 

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.