Jump to content

[SOLVED] Reading from an array


hotrod57

Recommended Posts

I have a text file that I read into an array using explode().  My separator is =.  I need to be able to look at the contents of $array[0] and $array[1] and create a form based on what is in the array.  For example: $array[0] = checkbox, checkbox, radio button and $array[1] = shoe, hat, car.  I need to be able to recognize that when I see the second "checkbox" in $array[0] it corresponds with "hat" in $array[1], and so on.  I know this seems simple, but the catch is the values in the text file are changeable, I need my function to be able to recognize the differences.  This time checkbox and shoe go together, but next time it could be a drop box with different values.  Does this make sense?

Link to comment
Share on other sites

Your original post and the sample code do not appear to make sense to me. In your first post you state that you are using the equal sign as a delimter and that the values of the array, after using explode, would be something like:

 

$array[0] = checkbox, checkbox, radio button

$array[1] = shoe, hat, car.

 

But according to your code above you are not delimiting those based upon the commas. Try this:

 

<?php
$lines = file('test_read.txt');

foreach ($lines as $line) {
    list($fields, $values) = explode("=", $line);

    $fields = explode(",", $fields);
    $values = explode(",", $values);

    for ($i=0; $i<count($fields); $i++) {

        switch(trim($fields[$i])) {

            case 'checkbox':
                print ("<input type=\"checkbox\" name=\"userbox\" value=\"".trim($value[$i])."\">".trim($value[$i])."");
                break;

            case 'radio':
                print ("<input type=\"radio\" name=\"userbox\" value=\"".trim($value[$i])."\">".trim($value[$i])."");
                break;

        }
    }
}
?>

Link to comment
Share on other sites

Thanks a lot!  That helped me out big time.  But now I have the two checkboxes with no values beside them.  Any suggestions?

 

<?php
$lines = file('test_read.txt');

foreach ($lines as $line) {
    list($fields, $values) = explode("=", $line);

    $fields = explode(",", $fields);
    $values = explode(",", $values);

    for ($i=0; $i<count($fields); $i++) {

        switch(trim($fields[$i])) {

            case 'checkbox':
                print ("<input type=\"checkbox\" name=\"userbox\" value=\"".trim($value[$i])."\">".trim($value[$i])."");
                break;

            case 'radio':
                print ("<input type=\"radio\" name=\"userbox\" value=\"".trim($value[$i])."\">".trim($value[$i])."");
                break;

        }
    }
}
?>

Link to comment
Share on other sites

And what exactly shall:

 

title = Test Form;
checkbox = shoe;
checkbox = hair;
radio_button = 3;
comment_box
dropbox = 18-29;
dropbox = 30-49;
dropbox = 50-75;
textbox = 50;

 

Please post the html form which you wanna produce from the above text.

Link to comment
Share on other sites

Ok, what you stated in your first post is not acurate based upon the data you are using. You will not get

 

$array[0] = checkbox, checkbox, radio button

$array[1] = shoe, hat, car.

 

from that data because the array is getting redefined on each itteration of the loop. So the first time through you would have $array[0]='checkbox' & $array[1]='shoe'. On the 2nd iteration it would be $array[0]='checkbox' & $array[1]='hat', etc.

 

Try this:

 

<?php
$lines = file('test_read.txt');

foreach ($lines as $line) {
    list($field, $value) = explode("=", $line);

    $value = substr(trim($values), 0, 1);

    switch(trim($field)) {

        case 'checkbox':
            print ("<input type=\"checkbox\" name=\"userbox\" value=\"$value\">$value");
            break;

        case 'radio':
            print ("<input type=\"radio\" name=\"userbox\" value=\"$value\">$value");
            break;

    }
}
?>

 

however, I don't know how that is going to work for you since all the field names will be the same. Hope you have some logic to handle that.

Link to comment
Share on other sites

Using mjdomato's code provided (which is very nice) here is an alternative:

 

<?php
$lines = file('test_read.txt');

foreach ($lines as $line) {
    list($field, $value) = explode("=", $line);

    $value = substr(trim($values), 0, 1);
    echo get_html($field, $value);

}

function get_html($tag, $value) {
    switch(trim($tag)) {
        case 'checkbox':
            return "<input type=\"checkbox\" name=\"userbox\" value=\"$value\">$value";
            break;

        case 'radio':
            return "<input type=\"radio\" name=\"userbox\" value=\"$value\">$value";
            break;
        // more cases below if needed.
    }
}
?>

 

That way its more versatile returning it because you can then use this in other sites and keep that code in a functions file.

 

Just another way of doing it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.