multi-dimensional array into select box

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

I have figured out how to read a regular array into a select drop down form element, but I'm running into logical issues with the multi-dimensional array.

<?php
if ($show == \"textlinks\" && $puzzle == \"\")
    {
        foreach (
$pictures as $category=>$data)
            {
    <h3> echo
$category; </h3>
    <p>
    foreach (
$data as $name=>$subdata) {
        extract(
$subdata);
        // now you can use
$id and $title
       
$ui = $id;
        <form action=\"puzzle2.php\" method=\"post\">
        <p>
         echo
$title; <br />
        <input class=\"small\" type=\"hidden\" name=\"c\"
value=\" echo
$category; \">
        <input class=\"small\" type=\"hidden\" name=\"puzzle\"
value=\" echo
$name; \">
        <input class=\"small\" type=\"hidden\" name=\"show\"
value=\"textlinks\">
        <select class=\"small\" name=\"piece\">
array_walk (
$z, optionlist);
// this goes through the array once for each value in it
        </select>
        <input class=\"small\" type=\"submit\" value=\"go!\">
        </p>
        </form>
          } //END foreach (
$data
    </p>
  } //END foreach
$pictures
}
?>

The part where the $title is currently is what I want to plunk into the option tags, where the value and name are put in automatically.

I have a function for the second one:

<?php
   
function optionlist ($option)
        {
        echo \
"<option value=\\"$option\\">$option</option>\n\";
        }
?>

And have augmented that function for the first one, but it doesn't seem to be what I need.

This is the same data arrays as the thread http://www.webmaster-forums.com/showthread.php?threadid=18504 and the page source can be found at carterjackson.com/joshua/puzzle2.phps and the display can be found at carterjackson.com/joshua/puzzle2.php?show=textlinks (the thumbnails won't have the same function).

What I'm trying to do, if it's not obvious, is have it look like this:

CATEGORY

drop down of titles
drop down of puzzle cuts
go button

CATEGORY

drop down of titles
drop down of puzzle cuts
go button

CATEGORY

drop down of titles
drop down of puzzle cuts
go button

And at some point I'm going to add in a table to make the display nicer, but still, that's where I'm at. Tips? Suggestions? Places to look that I'm not seeing?

Thanks!

Smiling Suzanne

P.S. some lines are artificially broken.

They have: 601 posts

Joined: Nov 2001

Thank goodness that Perl allows you to split your code from your HTML. PHP embeded with HTML tags looks so so confusing... :-\

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

oh will, you're so silly! I've seen Perl far more confusing than this. Wink

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

That's just a programming style. That would probably be considered Minimal PHP Style because you try to stay in HTML mode as often as possible. What I use is considered Maximal PHP Style because my whole file is within PHP tags, everything is echoed, nothing outside of PHP tags.

Anyway, try this...

<?php
$z
= array (
    \
"6_piece_classic\",
    \"12_piece_zigzag\",
    \"16_piece_blocks\");

$pictures = array (
    \"Joshua\" => array (
        \"tongue\" => array (
            \"id\" => \"358293\",
            \"title\" => \"New Year's Opinion\"),
        \"bighands\" => array (
            \"id\" => \"353027\",
            \"title\" => \"Big Hands I Know You're The One\"),
        \"wintersquash\" => array (
            \"id\" => \"369925\",
            \"title\" => \"Winter Squash\"),
        \"arrr\" => array (
            \"id\" => \"409483\",
            \"title\" => \"Arrr!\"),
        \"mycutemen\" => array (
            \"id\" => \"409484\",
            \"title\" => \"My Cute Men\",
            \"category\" => \"Joshua\")
        )
    );


if (
$show == \"textlinks\" && $puzzle == \"\") {
    foreach (
$pictures as $category=>$data) {
        echo \"<h3>
$category</h3>\n\";
        echo \"<p>\n\";
        echo \"<form action=\\"
puzzle2.php\\" method=\\"post\\">\n\";
        echo \"<p>\n\";
        echo \"
$title<br />\n\";
        echo \"<input class=\\"
small\\" type=\\"hidden\\" name=\\"c\\" value=\\"$category\\" />\n\";
        echo \"<input class=\\"
small\\" type=\\"hidden\\" name=\\"puzzle\\" value=\\"$name\\" />\n\";
        echo \"<input class=\\"
small\\" type=\\"hidden\\" name=\\"show\\" value=\\"textlinks\\" />\n\";
        echo \"<select class=\\"
small\\" name=\\"ui\\">\n\";
        foreach (
$data as $name=>$subdata) {
            extract(
$subdata);
            echo \"<option value=\"
$id\">$title</option>\n\";
        } //END foreach (
$data
        echo \"</select>\n\";
        echo \"<input class=\\"
small\\" type=\\"submit\\" value=\\"go!\\" />\n\";
        echo \"</p>\n\";
        echo \"<br />\n\";
        echo \"<select class=\\"
small\\" name=\\"piece\\">\n\";
        array_walk (
$z, optionlist);
        echo \"</select>\n\";
        echo \"</form>\n\";
        echo \"</p>\n\";
    } //END foreach
$pictures
}
?>

Mark Hensler
If there is no answer on Google, then there is no question.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

My problem is that I have to always keep in mind that my clients/users don't know PHP, barely HTML. The more I can leave out, the better, for them to edit the files. That and I hate typing \", lol...

So I had the right idea, just had it in the wrong place? doh!!

Although I'm happy that I'm managing to figure it out to some extent. You should have seen how flipping chipper I was when I found array_walk and got it working first try... lol!

Anyway, yes, that works just dandy, Mark, thanks!

carterjackson.com/joshua/puzzle2.php?show=textlinks

w00t!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.