Jump to content

Archived

This topic is now archived and is closed to further replies.

jwwceo

What order do I do this in...

Recommended Posts

I am making my first php mySQL project and I'm at the toughest part...I'm a little confused where to even start on this part...

my project has t-shirts being held in a databse. One of the tables in the database is just for all the colors that various shirts come in. Many shirts come in lots of different colors. So I have created a third table with just 2 fields (shirt_id and color_id) that will match the shirts with all the different colors thay come in...

However, on my page for adding a t-shirt to the database I would like to have all the available colors listed with checkboxes so when I add the shirt I can also click on all the colors they come in and submit everything at the same time...

can this be done at the same time???....it seems like if I haven't added the shirt first then there won't be a shirt_id to be added to the joined third table. Does this make sense...

Share this post


Link to post
Share on other sites
so basicaly you would use somthing simular to this

[code]<form action="" method="post">
<input type="text" name="shirt_id"><Br>
<input type="checkbox" name="color[]" value="red">Red<Br>
<input type="checkbox" name="color[]" value="green">Green<Br>
<input type="checkbox" name="color[]" value="blue">blue<Br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($_POST['submit']) {
$shirt_id=mysql_real_escape_string($_POST['shirt_id']);
foreach($_POST['color'] as $color) {
  $color=mysql_real_escape_string($color);
  $query=mysql_query("INSERT INTO table_name (shirt_id,color_id) VALUES('$shirt_id','$color')");
}
}
?>[/code]

That should work :) hope it atleast helps you.


Regards
Liam

Share this post


Link to post
Share on other sites
Nice. That worked awesome...THANKS!!!

any tips on how to write a little code for the edit page which will see if the color values should be checked already or not...I can't seem to get my head around this....been working too long...

Share this post


Link to post
Share on other sites
[code]$acolor=array();
$query=mysql_query("SELECT color_id FROM tablen_name WHERE shirt_id = '1'");
while ($row=mysql_fetch_array($query)) {
$acolor[] = $row['color_id'];
}

$query2=mysql_query("SELECT color FROM colors");
while ($row2=mysql_fetch_array($query2)) {
if (in_array($row2['color'],$acolor)) {
  echo "<input type=\"checkbox\" name=\"color[]\" checked=\"checked\" value=\"$row2[color]\">$row2[color]<Br>";
} else {
  echo "<input type=\"checkbox\" name=\"color[]\" value=\"$row2[color]\">$row2[color]<Br>";
}
}[/code]

that *should do the trick

had to rush tho else gf gonna lkill me if im much later picking her up! lol :)

have fun

Liam

Share this post


Link to post
Share on other sites

×

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.