Re: Using one submit button (long, rambling, near-total rewrite)
| From: | Hugh Bothwell | Date: | Wed, 17 Apr 2002 22:52:06 +0000 |
| Subject: | Re: Using one submit button (long, rambling, near-total rewrite) | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-93428@lists.php.net to get a copy of this message | ||
"Jennifer Downey" <jendowney@attbi.com> wrote in message
news:20020417161148.17138.qmail@pb1.pair.com...
> I have no takers on this one?
You would have more help if you didn't glumph
a whole whack of code in... it takes five minutes
just to sort out what's what.
> if I have on item it is fine. If I have two items it
> won't update the first items price but will the
> second. if I try to enter a price in the first items
> textbox it doesn't update and then deletes the
> second item's price.
It sounds like you are trying to return multiple
values to a single variable - ie, you need to
return an array, then iterate through the array
to process it.
> $uid = $row['uid'];
> $id = $row['id'];
> $name = $row['name'];
> $image = $row['image'];
> $iquantity = $row['quantity'];
> $itype = $row['type'];
> $iprice = $row['price'];
Look up the documentation on extract()
> if($update)
> {
> $eprice = '$price[]';
> $query = "UPDATE {$config["prefix"]}_shop SET price =
> '$eprice'
> where uid = {$session["uid"]} AND id = '$id'";
> $ret = mysql_query($query) or die(mysql_error());
if(isset($update) and is_array($price))
foreach($price as $id => $newval)
if ($newval != "") {
$query =
"UPDATE {$config['prefix']}_shop "
."SET price='$newval' "
."WHERE uid='{$session['uid']}' AND id='$id'
";
mysql_query($query) or die(mysql_error());
}
> echo "<TABLE BORDER='0' WIDTH='95%'
> CELLPADDING='0'
> CELLSPACING='0'><TR>";
> echo "<TD width=20%><img
> src='$image'></TD>";
> echo "<TD width=30%><font
> size=2>$name</font></TD>";
> echo "<TD width=20%><font
> size=2><CENTER>$iquantity</CENTER></font></TD>";
> echo "<TD width=30%><font size=2><CENTER><a
>
> href='$PHP_SELF?id=$id&remove=yes'>X</a></CENTER></font></TD>";
> echo "<TD width=30%><font size=2><CENTER><input
> type=\"text\"
> value=\"\" name=\"price[]\" size='8'
> MAXLENGTH='8'><BR></a></CENTER></font></TD>";
> echo "</TD></TR></TABLE>";
> echo "<input=\"hidden\" name=\"remove\"
> value=\"yes\">";
> }
(grin) you realize your column widths add to 130% ?
This is not necessarily a problem; rather, I point it out as
a symptom of poorly formatted and hard-to-follow code.
I often find it useful to write simple table-making functions
just so it's easier to follow what's going on... something
like
// adjustable indentation for prettyprinting
define("BEGINTABLE", "\n\t");
define("ENDTABLE", "\n\t");
define("BEGINROW", "\n\t\t");
define("ENDROW", "\n\t\t");
define("BEGINCELL", "\n\t\t\t");
define("ENDCELL", "");
define("BEGINCONTENTS", "");
function makeTable($content, $width="", $border=0) {
return
BEGINTABLE."<table"
.( $width != "" ? " width='$width'" : "")
." border='$border' cellspacing='0'
cellpadding='0'>"
.$content
.ENDTABLE."<\table>";
}
function makeRow($content) {
return
BEGINROW."<tr>"
.$content
.ENDROW."<\tr>";
}
function makeCell($content="", $width="") {
return
BEGINCELL."<td".($width != "" ? " width='$width'"
: "").">"
.($content != "" ? $content : " ")
.ENDCELL."</td>";
}
Then your code turns into
// separate out the recurrent formatting
$s = "<div style='font-size: larger; align=center;'>";
$e = "</div>";
$content = makeRow(
makeCell($s."Image".$e, "20%")
.makeCell($s."Name".$e, "30%")
.makeCell($s."Quantity".$e, "20%")
.makeCell($s."Remove Item".$e, "30%")
.makeCell($s."Price".$e, "30%")
);
// Wow, the odd total here is a lot more obvious!
$query =
"SELECT uid, id, name, image, type, quantity "
."FROM {$config["prefix"]}_shop "
."WHERE uid = {$session["uid"]}";
$res = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($res)) {
extract($row);
$content .= makeRow(
makeCell("<img src='$image'>")
.makeCell($s.$name.$e)
.makeCell($s.$quantity.$e)
.makeCell($s."<input type='checkbox' "
."name='rem[$id]' value='true'>".$e)
.makeCell($s."<input type='text' value='' "
."name='price[$id]' size='8'
maxlength='8'>".$e)
);
}
echo
"\n<form>"
.makeTable($content, "95%")
.$s
."<input type='reset' value='Clear form'>"
."<input type='submit' name='update' "
."value='Update And Remove'>"
.$e
."\n</form>";
... I have changed a few things; for one, instead
of removing items singly, I have refit a set of
checkboxes, returning an array of selected items
for removal.
Also, note my use of single-quotes inside the double-quoted
strings... I find this much easier to follow, instead of umpteen
dozen escaped double-slashes.
I hope this is of some use to you.
Hugh.