So here are some snippets of what I have come up with so far…
(Things seem to be working, but it feels like maybe my code is still too loopy…) :-/
Code Flow:
- First I start off grabbing the Section from the URL, and making sure it is valid.
- Then I determine the SubSections associated with the Section in the URL, like this…
$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{
// Error-Handling
}
- Next I build a listing of ALL Articles for the given Section. This is where it seems like I need a 2nd Nested Query inside the WHILE which builds the SubSections…
// 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);
// 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 area of my script, I have…
<!-- FEATURED SUBSECTIONS -->
<div id="boxFeaturedSubsections">
<h2>Featured</h2>
<?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
?>
Questions/Concerns:
1.) I’m not sure if I needed to use an Array above.
2.) The way I have it now, I query all of the Articles for a given Section and Dimension. While this number would likey be under 50, it seems like it would make more sense to do a query for each Sub-Section… (This is really what started this thread. Part of me wants to keep my Outer While loop, but inside of it, run a query based on the current Sub-Section in the loop. The logic being that I am only grabbing the Articles that apply to the current Sub-Section. Because this is a summary page, I don’t plan on having more than 5 Articles per Sub-Section box, because there will be a link like this “<< View all MARKET articles>>” at the bottom of each Sub-Section box. Still, I feel like I’m being sloppy here?!)
3.) It seems a little flaky how I had to add IF-THEN’s to handle the start of my HTML…
4.) It seems like my Array Variable names are rather LARGE in my HTML code…
5.) In general, it just seems like my code could be streamlined more. (Maybe not?!)
Hope you can follow my code snippets above, plus my comments/questions…
Sincerely,
Debbie