NYCPHP Meetup

NYPHP.org

[nycphp-talk] MySQL and spaces in data

Adam Maccabee Trachtenberg adam at trachtenberg.com
Fri Jun 20 01:37:14 EDT 2003


On Thu, 19 Jun 2003, Jeff wrote:

> To differentiate makes/models on the page I generate checkboxes that
> look like the following:
> 
> <input name="ALFA-ROMEO|GTV-6" type="checkbox" value="1" checked >
> <input name="ALFA-ROMEO|SPIDER VELOCE" type="checkbox" value="1">
> 
> First part gives me the manufacturer, second part gives me the model.

Why don't you make the input name an array value like:
"makes[ALFA-ROMEO]" and give it a value of "GTV-6" or "SPIDER VELOCE"?

Then, you can do something like:

foreach ($makes as $make => $model) {
   print "Dealer has an $make $model";
}

Right now, you're probably doing something like:

foreach ($_POST as $data) {
    $clean = str_replace($data, "_", "-");
    list($make, $model) = split($clean, "|");
    print "Dealer has a $make $model";
}

(I may have some function parameter orders mixed up here.)

Since the browser only sends back "checked" checkboxes to the server,
what's the point of setting a value of "1"? Things should be easier if
you set the value to an actually useful value, like say, the model, or
even the make and the model. Then, at least, you wouldn't need to
worry about the weird re-encoding of the data and the restriction on spaces.

> Keep in mind that I'm on php 4.1.x and it does not have htmlentities nor
> html_decode_entity.

FWIW, htmlentities() has been around forever, so it should be in
4.1. Besides, that's pretty easy to mimic with strtr():

$from = array('&', '<', '>', '"');
$to   = array('&amp;', '&lt;', '&gt;', '&quot;');
$html = strtr($ascii, $from, $to);

(Well, you may have to rearrange the order to do a double pass because
of the &, but this is a good start.)

-adam

-- 
adam at trachtenberg.com
author of o'reilly's php cookbook
avoid the holiday rush, buy your copy today!




More information about the talk mailing list