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 3 of 3
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,108
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Need help with Multi-Dimensional Array

    I hate arrays!!

    But it looks like they are needed to get done what I need, so here is hoping you guys can help me out?!


    Background:
    When a user clicks on a navigation tab on my website, they are taken to a Section landing page.

    In the main area of the page is a box showing "Featured Sub-Sections".

    And for each Sub-Section, there is a box which contains links to "Featured Articles".


    So, from a back-end database standpoint, I have these Data Entities...
    - Section
    - Dimension
    - Subsection
    - Article


    I am trying to get all of this data - which resides in several tables - from the back end, and display it on my web page, and it seems like a Multi-Dimensional Array is my best bet...


    (Did I mention that I hate working with arrays??)


    So here is an example of real data that would populate my Section landing page... (Actually, I am only showing Article links for one Sub-Section, but you get the point.)

    Code:
    Section: finance
    Dimension: featured-finance
    
    id	subsection
    ---	-----------
    1	economy
    2	markets
    3	tax-season
    
    
    id	subsection	slug					heading		summary
    ---	-----------	----------------------------------	--------	--------
    1	tax-season	save-your-taxes-for-a-cpa		Save Your...	Doing your own...
    2	tax-season	filing-dates-to-remember
    3	tax-season	how-to-avoid-filing-electronically
    4	tax-season	what-is-eftps
    5	tax-season	5-tax-shelters-you-need-to-know

    Can someone please make some recommendations on the best way to set up my Array(s) to accomplish what I want??


    (BTW, I was able to come up with a really crude working example last night just using a query result-set for *one* Sub-Section, but where I am completely over-my-head- is trying to figure out how to loop through the needed data, while also laying out my HTML...)

    Hope all of that makes sense?!

    Sincerely,


    Debbie

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    could you just put an IF in the while loop on the dataset?

    something like:


    PHP Code:
    $subs mysql_query("select * from finance");
    $cont mysql_query("select * from featured-finance");

    while (
    $subcats mysql_fetch_array($subs)){
        while (
    $cats mysql_fetch_array($cont)){
            if (
    $cats['subsection'] == $subcats['subsection']){
                echo 
    $cats['slug'];
            }

        }
        
    mysql_data_seek($cont0);


    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,108
    Thanks
    27
    Thanked 0 Times in 0 Posts
    I'm not able to post my entire script here, but here is a pretty good chunk...

    (I would really value some feedback from all you PHP gurus out there!!)


    Script Process Flow:
    - First I check the URL for a "Section" and make sure it is valid.

    - If it is, then I run Query #1 to get all related "Subsections"...
    PHP Code:
        // Set Variables.
        
    $sectionSlug $_GET['section'];
        
    $sectionName getSectionName($dbc$sectionSlug);
        
    $dimensionSlug 'featured-' $sectionSlug;


        
    // ********************************
        // Build Featured Sub-Sections.        *
        // ********************************

        // Build query.
        
    $q1 "SELECT ss.slug, ss.name
                FROM section_dimension AS sd
                INNER JOIN dimension_subsection AS ds
                ON sd.dimension_slug = ds.dimension_slug
                INNER JOIN subsection AS ss
                ON ss.slug = ds.subsection_slug
                WHERE sd.section_slug = ?
                AND sd.dimension_slug = ?
                ORDER BY sd.section_slug, ss.sort"
    ;

        
    // Prepare statement.
        
    $stmt1 mysqli_prepare($dbc$q1);

        
    // Bind variable to query.
        
    mysqli_stmt_bind_param($stmt1'ss'$sectionSlug$dimensionSlug);

        
    // Execute query.
        
    mysqli_stmt_execute($stmt1);

        
    // Store results.
        
    mysqli_stmt_store_result($stmt1);

        
    // Check # of Records Returned.
        
    if (mysqli_stmt_num_rows($stmt1)>0){
            
    // Featured Sub-Sections Found.

            // Bind result-set to variables.
            
    mysqli_stmt_bind_result($stmt1$subsectionSlug$subsectionName);

            
    // Fetch below in loop...

        
    }else{
            
    // Featured Sub-Sections Not Found.
        


    - Next, I run Query #2 which finds all "Subsections" for the given Section/Dimension combination... (For example, Section='Finance' and Dimension='Featured Finance')
    PHP Code:
        // ****************************
        // Build Featured Articles.        *
        // ****************************

        // Build query.
        
    $q2 "SELECT ap.ds_subsection_slug, a.slug, a.heading, a.summary, a.image
                FROM article AS a
                INNER JOIN article_placement AS ap
                ON a.slug = ap.article_slug
                WHERE ap.sd_section_slug = ?
                AND ap.ds_dimension_slug = ?
                ORDER BY ap.ds_subsection_slug, RAND()"
    ;

        
    // Prepare statement.
        
    $stmt2 mysqli_prepare($dbc$q2);

        
    // Bind variable to query.
        
    mysqli_stmt_bind_param($stmt2'ss'$sectionSlug$dimensionSlug);

        
    // Execute query.
        
    mysqli_stmt_execute($stmt2);

        
    // Store results.
        
    mysqli_stmt_store_result($stmt2); 

    **NOTE: This is one place where I feel like I could do a better job, because I am returning *all* Articles for the given SECTION instead of just the Articles that pertain to a given SUBSECTION.

    Technically it isn't such a big deal, since the purpose of this page to simply display all Subsections for a given Section, and within each Subsection box, display maybe 5 "Featured Articles" (links).


    - Next, I build an array from Query #2...
    PHP Code:
        // Check # of Records Returned.
        
    if (mysqli_stmt_num_rows($stmt2)>0){
            
    // Featured Sub-Sections Found.

            // Bind result-set to variables.
            
    mysqli_stmt_bind_result($stmt2$articleSubsectionSlug$articleSlug$articleHeading$articleSummary$articleImage);


            
    // ********************************
            // Build Featured-Articles Array.    *
            // ********************************
            
    $n=1;
            while (
    mysqli_stmt_fetch($stmt2)){
                
    $featuredArticlesArray[$n] = array('articleSubsectionSlug' => $articleSubsectionSlug,
                            
    'articleSlug' => $articleSlug,
                            
    'articleHeading' => $articleHeading,
                            
    'articleSummary' => $articleSummary,
                            
    'articleImage' => $articleImage);
                
    $n=$n+1;
            }
    //End of BUILD FEATURED-ARTICLE ARRAY

            // Fetch below in loop...

        
    }else{
            
    // Featured Sub-Sections Not Found.
        


    - Finally, down in the HTML portion of this script, I have this code...
    PHP Code:
        <?php
            
    // ********************************
            // Build Featured Sub-Sections.        *
            // ********************************
            
    while (mysqli_stmt_fetch($stmt1)){
                
    // Start of Sub-Section Box.
                
    echo "<div class='boxSubSection'>
                        <h3>$subsectionName</h3>"
    ;

                
    // ****************************
                // Build Featured Articles.        *
                // ****************************
                
    $a 1;
                foreach(
    $featuredArticlesArray as $articleNo => $articleArray){

                    
    // ********************************
                    // Find Articles in Sub-Section.    *
                    // ********************************
                    
    if ($articleArray['articleSubsectionSlug'] == $subsectionSlug){
                        
    // Match.
                        
    if ($a == 1){
                            
    // Build Featured-Article.
                            
    echo "<div class='subFeatured'>
                                <a href=''>
                                {$articleArray['articleImage']}
                                <h4>{$articleArray['articleHeading']}</h4>
                                </a>
                                {$articleArray['articleSummary']}
                                </div>"
    ;

                        }else{
                            if (
    $a == 2){
                                
    // Start of Featured-Links.
                                
    echo "<div class='subArticles'>
                                        <ul>"
    ;
                            }

                            echo 
    "            <li>
                                            <a href='/$sectionSlug/{$articleArray['articleSubsectionSlug']}/{$articleArray['articleSlug']}'>{$articleArray['articleHeading']}</a>
                                            </li>"
    ;
                        }
                        
    $a $a 1;

                    }
    //End of FIND ARTICLES IN SUBSECTION

                
    }//End of BUILD FEATURED ARTICLES

                // End Featured-Links
                
    echo "                </ul>
                                        </div>"
    ;

                
    // End of Sub-Section Box.
                
    echo "</div>";

            }
    //End of BUILD FEATURED SUB-SECTIONS
        
    ?>

    When I run my script, it seems to be working okay, but I just feel like my code isn't as efficient as it could be.

    This is v2.0, and maybe for v3.0, I will finally learn OOP and MVC, but for now, I am just trying to write the best and *cleanest* Procedural Code possible!!

    Your feedback and suggestions would be appreciated!!!

    Sincerely,


    Debbie


  •  

    Posting Permissions

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