Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Multiple Arrays in PHP Echo

    Hi All,

    I'm new to these forums, I registered because I'm having a problem I know should be simple I just cannot find anything online. Perhaps I do not know what to search but I'm not finding anything on my problem.

    I have a PHP echo where I want to have another PHP echo inside of that displays all options (in the form of a dropdown) from a different table.

    I know you can do ;

    PHP Code:
    <?php 
    while($row mysql_fetch_array($arrayOne))
    {
    echo  
    "Stuff Here"."Stuff Here also";}?>
    But I want the Second Area to have for example the SQL Query from $arrayTwo , not arrayOne.

    I hope I explained this well enough, if someone could just point me in the right direction.

    Cheers

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I'm not sure I understand your question.
    If $arrayTwo is the one you want, you can just swap the $arrayOne for the $arrayTwo resultset. Note if its already been iterated you must reset the resultset with mysql_data_seek.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    BlueMug (02-11-2014)

  • #3
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I have a back end database where the PHP echo's all the values in the database and I can edit all the values. However I want to add a Dropdown list that draws values from a seperate table so I cannot change the value to anything I do not have in that table.

    PHP Code:
    <?php 
    while($row mysql_fetch_array($shirtsArray))
    {
    echo 
    "
       <form id=\"form\">
         <select name=\"colours\" id=\"colours"
    .$row['shirts_id']."\">
            <option>"
    .$row['colours']."</option>
        </select>

    ....
    "
    ;
    }
    ?>
    So It shows all these different values, but for the dropdown I want it to show all the colours from the "colour table". In theory..

    PHP Code:
    <?php 
    while($row mysql_fetch_array($shirtsArray))
    {
    echo 
    "
       <form id=\"form\">
         <select name=\"colours\" id=\"colours"
    .$row['shirts_id']."\">
            <?php while($row = mysql_fetch_array($shirtsArray))
    { echo" 
    <option>".$row['colours']."</option>";
               }?>
        </select>

    ....
    "
    ;
    }
    ?>
    I want it to work like that, but I know that is incorrect but just to maybe make it clearer of what I want it to do..

  • #4
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Sorry pretend the second mysql_fetch_array in the second php chunk is "coloursArray"

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Posts
    456
    Thanks
    0
    Thanked 20 Times in 20 Posts
    I'm guessing he wants to get the list of colors from table2 that are asscociated with shirtID from table1 loop?
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • Users who have thanked c1lonewolf for this post:

    BlueMug (02-11-2014)

  • #6
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by c1lonewolf View Post
    I'm guessing he wants to get the list of colors from table2 that are asscociated with shirtID from table1 loop?
    Exactly

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    If you want to link the colours from the colour table to the records in the first table then you can do a join. You can do left and right joins or just normal joins.

    See tizag.com for examples of mysql joins.

    Failing that, you could simply run two different queries if you don't feel confident with joins and simply run the second loop inside the first. That may actually be the better option here because otherwise you'll have colours in rows and only one of them will contain the data from the main table.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    BlueMug (02-11-2014)

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Edit:
    The more I think of this, the more it doesn't make sense to me to pull the colours out of the table based on items. Methinks there's a normalization issue here.
    What data exists in the colour property?


    Associated isn't the right word here methinks. There's a relationship to be sure, but the intent here is to retrieve all the possible options.
    You can certainly iterate the resultset over and over, but I would not. I would sacrifice the memory required to create a variable so as to allow me a single iteration.
    The easiest way to do this and keep it legible:
    PHP Code:
    $aColours = array();
    while (
    $row mysql_fetch_assoc($shirtsArray)) // its not really an array though, its a resultset
    {
        
    $aColours[] = sprintf('<option value="%s">%s</option>'$row['colour'], $row['colour']);
    }
    mysql_data_seek($shirtsArray0);
    $sColours implode(PHP_EOL$aColours);
    while (
    $row mysql_fetch_assoc($shirtsArray))
    {
       
    printf("<form id=\"form\">
         <select name=\"colours\" id=\"colours%s\">
            %s
        </select>"
    $row['shirts_id'], $sColours);

    Something like this?

    Edit:
    Oh also, it may be worth instead storing just an array of colours, and writing a function for it. This way you can pull the selected as well:
    PHP Code:
    function availableColoursOptions(array $aColours$sSelected null)
    {
        
    $sResult '';
        foreach (
    $aColours AS $colour)
        {
            
    $selected '';
            if (!empty(
    $sSelected) && strcmp($colour$sSelected) == 0)
            {
                
    $selected ' selected="selected"';
            }
            
    $sResult .= sprintf('<option value="%s"%s>%s</option>'$colour$colour$selected);
        }
        return 
    $sResult;
    }

    // . . .
       
    printf("<form id=\"form\">
         <select name=\"colours\" id=\"colours%s\">
            %s
        </select>"
    $row['shirts_id'], availableColoursOptions($aColours$row['colour'])); 
    Uncertain if its contained, but meh.

    Edit:
    Hah oops, and the original array would be without the option markup code.


    Last edited by Fou-Lu; 02-11-2014 at 12:18 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    BlueMug (02-11-2014)

  • #9
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Edit:
    The more I think of this, the more it doesn't make sense to me to pull the colours out of the table based on items. Methinks there's a normalization issue here.
    What data exists in the colour property?


    Associated isn't the right word here methinks. There's a relationship to be sure, but the intent here is to retrieve all the possible options.
    You can certainly iterate the resultset over and over, but I would not. I would sacrifice the memory required to create a variable so as to allow me a single iteration.
    The easiest way to do this and keep it legible:
    PHP Code:
    $aColours = array();
    while (
    $row mysql_fetch_assoc($shirtsArray)) // its not really an array though, its a resultset
    {
        
    $aColours[] = sprintf('<option value="%s">%s</option>'$row['colour'], $row['colour']);
    }
    mysql_data_seek($shirtsArray0);
    $sColours implode(PHP_EOL$aColours);
    while (
    $row mysql_fetch_assoc($shirtsArray))
    {
       
    printf("<form id=\"form\">
         <select name=\"colours\" id=\"colours%s\">
            %s
        </select>"
    $row['shirts_id'], $sColours);

    Something like this?

    Edit:
    Oh also, it may be worth instead storing just an array of colours, and writing a function for it. This way you can pull the selected as well:
    PHP Code:
    function availableColoursOptions(array $aColours$sSelected null)
    {
        
    $sResult '';
        foreach (
    $aColours AS $colour)
        {
            
    $selected '';
            if (!empty(
    $sSelected) && strcmp($colour$sSelected) == 0)
            {
                
    $selected ' selected="selected"';
            }
            
    $sResult .= sprintf('<option value="%s"%s>%s</option>'$colour$colour$selected);
        }
        return 
    $sResult;
    }

    // . . .
       
    printf("<form id=\"form\">
         <select name=\"colours\" id=\"colours%s\">
            %s
        </select>"
    $row['shirts_id'], availableColoursOptions($aColours$row['colour'])); 
    Uncertain if its contained, but meh.

    Edit:
    Hah oops, and the original array would be without the option markup code.


    Thanks for the input! It's a little late now and I don't feel like getting all my files up and doing some tests or I'll be up for a while.

    To clarify, I just used the colour and shirts example for the sake of this post to simplify it. But lets say I have a table where I can add/edit/delete new colours anytime I want. So I have Red, Yellow, Blue in that table. I do not want to be able to put a colour I do not have in my colours table When I'm adding/editing the shirts colour column in my shirts table. So I just want those colours that I have in my colour table to appear as a dropdown in my shirts table, and then if If I get new colours all I would have to do is add the colour to my colour table and then I would have the ability to add that new colour because the colour column is a dynamic dropdown pulling data from that other table.


    If you want to link the colours from the colour table to the records in the first table then you can do a join. You can do left and right joins or just normal joins.

    See tizag.com for examples of mysql joins.

    Failing that, you could simply run two different queries if you don't feel confident with joins and simply run the second loop inside the first. That may actually be the better option here because otherwise you'll have colours in rows and only one of them will contain the data from the main table.
    Thank you as well! I will look up MySQL joins tomorrow morning. How exactly could I run a Loop inside of a loop? I have tried that and always get errors and looked online and can't seem to find an example.

    Thanks for all the help everyone!

  • #10
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks for all the help everyone!

    I think I was terrible as explaining my question but I was working on it today and looked into some of the things you suggested. I ended up going with a mysql_data_seek..

    If anyone is interested this is what I did...

    PHP Code:
    <?php 
    while($row mysql_fetch_array($shirtResults))
    {
    echo 
    "
       <form id=\"editForm\">
    <tr>
    <td>
         <select name=\"colours\" class=\"small marginDown\" id=\"colour.$row['shirts_id']."">
            <option selected>"
    .$row['colours']."</option>
            <option>--</option>"
    ;
            
    mysql_data_seek($colourResults,0);
            while(
    $rowColour mysql_fetch_array($colourResults)){
                if(
    $row['colours'] == $rowColour['colours']){ $sel 'selected'; }else{ $sel ''; }
                echo 
    '<option value="'.$rowColour['colours'].'" '.$sel.'>'.$rowColours['colours'].'</option>';
            }
        echo 

        </select>
               **REST OF ORIGINAL WHILE LOOP HERE **

  • #11
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Solved

    I dont have the option to edit and make this posted RESOVLED. Maybe an admin if he sees this can change this to resolved. Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •