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 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2017
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to build a nested foreach??

    Hi!

    I have had a site for a long time, had to change host, then one of my scripts broke

    I have had a nested foreach statement with if statements inside, this script give some empty results and are not optimized in any way, my new host has some settings I canīt figure out:/

    So, my real question is then..

    How do I make a nested foreach statement to fit my needs? (Or make ONE mysql request and sort it according to below instruction?)

    I want to make a list, this list should be sorted in various ways, I want to sort according to several conditions see example, eg:

    I want to show all rows, first all MEN, then all WOMEN, within each gruop of men and women I want to show all BLONDS then all DARKS, within all blonds I want to show all BLUEEYES and then all BROWNEYES and then all GREENEYES, within all eye color I want sort the SHORT first and then the TALLS. Result should look like:

    foreach ALL MEN
    - foreach BLONDS
    -- foreach BLUEEYES
    --- foreach SHORT
    --- foreach TALLS

    - foreach BLONDS
    -- foreach BROWNEYES
    --- foreach SHORT
    --- foreach TALLS

    - foreach BLONDS
    -- foreach GREENEYES
    --- foreach SHORT
    --- foreach TALLS

    - foreach DARKS
    -- foreach BLUEEYES
    --- foreach SHORT
    --- foreach TALLS

    - foreach DARKS
    -- foreach BROWNEYES
    --- foreach SHORT
    --- foreach TALLS

    - foreach DARKS
    -- foreach GREENEYES
    --- foreach SHORT
    --- foreach TALLS

    foreach ALL WOMEN
    - foreach BLONDS
    -- foreach BLUEEYES
    --- foreach SHORT
    --- foreach TALLS

    - foreach BLONDS
    -- foreach BROWNEYES
    --- foreach SHORT
    --- foreach TALLS

    - foreach BLONDS
    -- foreach GREENEYES
    --- foreach SHORT
    --- foreach TALLS

    - foreach DARKS
    -- foreach BLUEEYES
    --- foreach SHORT
    --- foreach TALLS

    - foreach DARKS
    -- foreach BROWNEYES
    --- foreach SHORT
    --- foreach TALLS

    - foreach DARKS
    -- foreach GREENEYES
    --- foreach SHORT
    --- foreach TALLS

    Maybe I have missed some results above, the idea is to have a nested foreach statement OR ONE mysql request that sort my result so that I only have to use ONE foreach and the result shows on the screen.

    Today I send the info througt the URL like param1=MEN&ASCDESC1=DESC&param2=WOMEN&ASCDESC2=ASC... and use a GET in my mysql request.
    It does not matter if the params is stored in a table/column/something else or if it is passed through URL as long as it works And the sort params should be dynamic, it can be 1 or 15 params

    Hope someone can help me!

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,360
    Thanks
    10
    Thanked 1,189 Times in 1,179 Posts
    You said after you switched hosts, one of your scripts no longer works. You are not specific on if the failure is a "warning" or "fatal".

    Show us a listing of that script.

    I'm going to guess your "new" webhost does not support MySQL, as in obsolete MySQL.
    You will now need to use PDO, or in the least, MySQLi (i=improved).

    Won't know much more until we see the script that is in question.

  3. #3
    New Coder
    Join Date
    Oct 2017
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, thank you for the answer!

    My whole site will be rebuilt eventually.

    So the info about the host is redundant.

    My real question is the rest, how do I build a logic in PHP (alternativ a mysql request) to meet my specs?

  4. #4
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,360
    Thanks
    10
    Thanked 1,189 Times in 1,179 Posts
    Do you have an existing SQL (what you call MySQL) database with tables and columns of data?
    You'll need a database with all of the information. And tell us how you structured it.

    For PDO, this is a place to read about it: https://phpdelusions.net/pdo

    I'm going to assume you have a row for each person with all of their traits in columns?
    For sure we need to see how you structured your SQL table(s).


    gender|age|height|weight|haircolor|eyecolor|etc|etc|etc

  5. #5
    New Coder
    Join Date
    Oct 2017
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi again!

    Yes, everything is in place

    I have many tables and in this perticular table I have many columns lika you describe, so the fetching from db is working, the only thing I donīt know right now is how to build the logic in PHP to fit my needs
    Today I had a thought about arrays and classes!
    Instead of sorting the result in foreaches, maybe it is doable to handle the array, to sort the result in the array, is that better?

    Regardles of what is the best way is I will take it

  6. #6
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,588
    Thanks
    10
    Thanked 161 Times in 156 Posts
    How about lets start with your database and getting the data out if the way you need it. Post or PM an SQL dump of your DB so we can review it. If the DB is not correct there is no point writing any code until it is.
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    Make A Donation https://www.paypal.me/KevinRubio

  7. #7
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,360
    Thanks
    10
    Thanked 1,189 Times in 1,179 Posts
    Holy Cats! Or Holy Dogs!

    Marcus PM'd me a sample of the table ... it's a lot of columns, and a lot of data.

    There are so many combinations and so many columns that I think the results of the query will be overwhelming.

    On sites where a company has thousands of items, example light bulbs, they have a 'filtering search' where the user clicks check boxes or selects drop-down items to narrow down the search. The results update as each filter setting "makes" or "breaks" the search result. Looking at your table, if I were to check the "male dog" box, half of the results would go away immediately. Then if I checked the "dark" box, another half might drop off. It's all AJAX/JQuery so as the filters are clicked, the results change in real time. Is this something you are attempting?

  8. #8
    New Coder
    Join Date
    Oct 2017
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you for the reply!

    Yes and no actually

    My needs are really for about 7 params, not more than that
    I just thought that I ask a question that is generalize it I would get an answer that is generalized

    So if I modify my question, how do I build a logic to sort according to my needs and limit the variables by 7?


 

Tags for this Thread

Posting Permissions

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