Jump to content


Photo

What order do I do this in...


  • Please log in to reply
3 replies to this topic

#1 jwwceo

jwwceo
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 28 August 2006 - 07:59 AM

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...

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 28 August 2006 - 08:44 AM

so basicaly you would use somthing simular to this

<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')");
 }
}
?>

That should work :) hope it atleast helps you.


Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 jwwceo

jwwceo
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 28 August 2006 - 09:20 AM

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...

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 28 August 2006 - 10:02 AM

$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>";
 }
}

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users