Page 1 of 1

a quick simple one

Posted: Fri Jan 16, 2004 12:28 pm
by dmcglone
I can't seem to get the line to break in the dropdown for some reason.
Im trying to put each size on a new line in the select box. I've put <br> tags everywhere and it doesn't work for some reason.

Could anyone yell at me and kick me in the rearend and then give me a hand. :?

if($row['SIZE'] != 'n/a'){
echo "<td align=\"center\" height=20 width=120 valign=top>";
echo "<select>";
echo "<option value=\"$SIZE\">$row[SIZE]</option>";
echo "</select>";
echo "</td>";
}

Posted: Fri Jan 16, 2004 1:38 pm
by kettle_drum
<select name="blah">
<option value="<?php echo $size; ?>"><?php echo $size; ?>
<option value="<?php echo $size2; ?>"><?php echo $size2; ?>
<option value="<?php echo $size3; ?>"><?php echo $size3; ?>
</select>

Etc. The lines will seperate themselves.

Posted: Fri Jan 16, 2004 1:46 pm
by ilovetoast
Anywhooo, here's some code I wrote since I'm bored:

Code: Select all

<?php

// made up data -- i get to make it up since you didn't give any ;)
$row		= array ();
$num_rows	= 5;

$sizes		= array ();
$sizes&#1111;0]	= "unsatisfying";
$sizes&#1111;1]	= "average";
$sizes&#1111;2]	= "large";
$sizes&#1111;3]	= "n/a";
$sizes&#1111;4]	= "as big as my arm";

echo "<td align="center" height=20 width=120 valign=top>"; 
echo "<select>"; 

for ($i = 0; $i < $num_rows; $i++) &#123;
	$row&#1111;'SIZE']	= $sizes&#1111;$i];
	$SIZE			= $i;
	
	if ($row&#1111;'SIZE'] != 'n/a') &#123; 
	echo "<option value="$SIZE">$row&#1111;SIZE]</option>"; 
	&#125;
&#125;

echo "</select>"; 
echo "</td>";

?>
peace

Posted: Fri Jan 16, 2004 3:20 pm
by dmcglone
Does it matter that I have all the sizes pertaining to that perticular product in the same table field?

if so would the code still be the same? 8O

Posted: Fri Jan 16, 2004 3:26 pm
by ilovetoast
Question one - yes.

Question two - no.

If you post the following I'll write the exact code you need (one version at least):
2 sample rows of data. Make something up if you need to. Ignore columns not used in this select.

What do you want in the value tag? A number, the size itself?
I assume you want the size itself in the select for people to see.
What's your delimiter for the assorted sizes if there's more than one in the size column? A comma?
And last, what db? MySQL?

peace

Posted: Sat Jan 17, 2004 7:27 am
by dmcglone
I appreciate the offer, but if I let you write the code, I will not learn how to do this. This whole thing is for learning purposes.

QUOTE:
2 sample rows of data. Make something up if you need to. Ignore columns not used in this select.

only column used is SIZE.

As for your questions:

I would like the value tag to show the size itself.
a comma would work fine for a delimiter.
and last the db is MySQL.

Instead of writing the code for me, could you give me some ideas how I would accomplish this task instead? or would it be too hard to explain? is this Ok?

Thanks
David

Posted: Sat Jan 17, 2004 11:21 am
by ilovetoast
OK well here's how you get started:

Make sure you have your db query setup properly, and in working order.

Once you have the query, you'll want to execute it with the function mysql_query(). Place the results into a variable such as $results.

Now you set up your while loop. Use the function mysql_fetch_array(). There is a nice example on that page that show just how to formulate the while loop.

Before you can echo each option for the select, you'll going to have to split apart the SIZE field based on your delimiter of choice. This is done inside the while loop, as you have to do it each time. Check out explode(). You could also use preg_split or split, but in the absence of the need for a regular expression, explode will be the fastest option. Put the results of the explode function into an array.

Next you'll need a new while loop. The first while loop went through each item in you quesry result, this one goes inside that one and loops through each exploded SIZE for the corresponding row.

Finally, put the echo statement in now. As the inner while loop iterates, it will output the correct option for your select. When it finishes the outer loop will iterate and repeat the entire process for the next row in the result.

Post your code as you go if you need help.

peace

Posted: Sun Jan 18, 2004 11:02 am
by dmcglone
Awesome!

Thanks, you got me on the right track, and I learned something.

What do you think of this?:

$size = "$row[SIZE]";
$sizes=explode(", ", $size);
if($size != 'n/a'){
echo "<td align=\"center\" height=20 width=120 valign=top>";
echo "<select>";
foreach ($sizes as $size)
echo "<option value=\"$size\">$size<br /></option>";
echo "</select>";
echo "</td>";
}

else{
echo "<td align=\"center\" height=20 width=120 valign=top></td>";
}

is this good coding?

David

Posted: Sun Jan 18, 2004 11:14 am
by markl999
It's getting there ;) Without specifically referring to your code (as i know you want to find your own way, which is both great and refreshing ;)) here's a few tips i find useful.

You don't need to quote variables when 'just using them'...e.g
$foo = $bar; instead of $foo = "$bar";

Try not to create temporary variables if you don't need to, e.g
$foo = $bar ... why not just use $bar ?

You can do..
echo '<a href="blah">foo</a>'; instead of having to escape the quotes as in..
echo "<a href=\"blah\">foo</a>";

If you have 'chunks' of HTML then it's usually better to just break out of PHP mode instead of echo'ing all the HTML, e.g
<?php
...some PHP code here...
?>
....some HTML here...
<?php
some more PHP here
?>

Using error_reporting(E_ALL); at the top of a script (or set by default in php.ini or a vhost section in httpd.conf) will catch useful warnings/notices about undefined variables etc.

Posted: Sun Jan 18, 2004 12:16 pm
by dull1554
or for big chunks of html in a php script......HEREDOC

ex.

Code: Select all

//open heredoc
Print <<< ANYSRTING
//all ur HTML
//close heredoc
ANYSTRING;

Posted: Mon Jan 19, 2004 9:20 am
by dmcglone
Thanks for the tip mark, I know exactly what your saying. I have used single quotes here and there and double quotes here and there, and my main problem is understanding most of the special characters in PHP. I figure them out but then forget them sometimes. for instance

$foo = "$foobar" assigns "$foobar" to "$foo" and
echo $foo will echo it to the screen.

but,

echo '$foo' will echo "$foo" to the screen instead of the variable that is in it am I correct? Im pretty sure its something like that. And listen to me right now, thats what my problem is. :D

I also have a big problem of when and when not to use a comma,and when and when not to use parenthesis( ).

and also a problem with lines that use more than 1 parenthesis such as (($var) $var2) or something like that.

Posted: Mon Jan 19, 2004 10:54 am
by vigge89
for chunks of HTML-code, i ussually do the same as i do for one-lines of echo, this:

Code: Select all

<?php
//entering echo
echo "
<HTML>
<BODY>
<a href='somepage.php'>Hello</a>
You can use variables to!
some variable is set to $var
your ip: {$_SERVER['REMOTE_ADDR']}
"; //exiting echo

?>
you dont need to use \" in echo, instead, use ' ;)

Posted: Mon Jan 19, 2004 11:02 am
by twigletmac
vigge89 wrote:for chunks of HTML-code, i ussually do the same as i do for one-lines of echo, this:

Code: Select all

<?php
//entering echo
echo "
<HTML>
<BODY>
<a href='somepage.php'>Hello</a>
You can use variables to!
some variable is set to $var
your ip: {$_SERVER['REMOTE_ADDR']}
"; //exiting echo

?>
you dont need to use " in echo, instead, use ' ;)
That would be better in a heredoc block - then you can use double quotes and single quotes and not have to worry about escaping either.

Mac

Posted: Mon Jan 19, 2004 11:09 am
by dmcglone
vigge89 wrote:for chunks of HTML-code, i ussually do the same as i do for one-lines of echo, this:

Code: Select all

<?php
//entering echo
echo "
<HTML>
<BODY>
<a href='somepage.php'>Hello</a>
You can use variables to!
some variable is set to $var
your ip: {$_SERVER['REMOTE_ADDR']}
"; //exiting echo

?>
you dont need to use " in echo, instead, use ' ;)
I usually do the opposite like this:

<?php
//entering echo
echo '
<HTML>
<BODY>
<a href="somepage.php">Hello</a>
You can use variables to!
some variable is set to $var
your ip: {$_SERVER['REMOTE_ADDR']}
'; //exiting echo

?>

Posted: Mon Jan 19, 2004 11:13 am
by twigletmac
dmcglone wrote: I usually do the opposite like this:

<?php
//entering echo
echo '
<HTML>
<BODY>
<a href="somepage.php">Hello</a>
You can use variables to!
some variable is set to $var
your ip: {$_SERVER['REMOTE_ADDR']}
'; //exiting echo

?>
That'll cause you problems because PHP won't parse that at all - you'll get the literal output instead of the variables replaced.

Mac