An associative array is an excellent suggestion and Debbie gets to play with curly braces.
You code implies something that is STATIC.
My situation is DYNAMIC.
If this was just hard-coding 4 slots for Sub-Sections, I would have never posted here.
I donāt particularly want to read the code for several reasons:
1.- You havenāt disclosed the table structure making it impossible to evaluate the sql
2.- I havenāt yet got to āpreparedā statements, Iām currently working on class architectue.
3.- If and when you reorganize your page the code is likely to be different.
How can you help if you refuse to look at my code?
I didnāt disclose my ERD and Table Structure because as I said before, it is immaterial.
As for your comments aboveā¦
1.) Conceptually all you need to know is that a Section has multiple SubSection which have multiple Articles. (Iām not trying be a jerk on this, but the ERD for what we are talking about is 7 tables, and I donāt want this thread veering off into a debate about my Data Model which is fine.)
2.) Prepared Statements are not what this thread is about, so no knowledge is really required.
3.) If I rearrange anything, my code will likely be different, so what is your point?!
You donāt need the $n=$n+1; The name C++ comes from the ++ operator. You could use
$featuredArticlesArray[$n++]
which would increment $n after it was used. ++$n would increment $n before it was used.
All well and good, but not what my OP is about.
Sincerely,
Debbie
I knew there had to be more to your sample layout than that single div.
<div class=āboxSubSectionā></div>
Probably the best way to make each section dynamic is replace what youāve already made for your link list (assuming <ul><li></li></ul>) with the foreach loop from your array.
I would think you wouldnāt need that āWHILEā loop in the html.
Just add the $subsectionSlug as the primary key in your array building.
This would let you do away with lines like
if ($articleArray[āarticleSubsectionSlugā] == $subsectionSlug){
Doing away with the $n keys would also be a good idea and use undefined keys (0,1,2 etc).
This would let you know that key[0] is your featured acticle and 1 through 5 (or how many you wish to show) would be article links.
Itās a little hard to put this together without being able to test it but your array building section might be something like this assuming you are still nesting these queries,
<?php
$sectionSlug = $_GET['section'];
$sectionName = getSectionName($dbc, $sectionSlug);
$dimensionSlug = 'featured-' . $sectionSlug;
// Build Featured Sub-Sections.
$featuredArticlesArray = array();
// 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
}
// 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[$subsectionSlug]['subsectionName'][] = $subsectionName;
$featuredArticlesArray[$subsectionSlug]['articleSlug'][] = $articleSlug;
$featuredArticlesArray[$subsectionSlug]['articleHeading'][] = $articleHeading;
$featuredArticlesArray[$subsectionSlug]['articleSummary'][] = $articleSummary;
$featuredArticlesArray[$subsectionSlug]['articleImage'][] = $articleImage;
}//End of BUILD FEATURED-ARTICLE ARRAY
// Fetch below in loop...
}else{
// Featured Sub-Sections Not Found.
}
?>
The output section might be something like this
<?php
// ********************************
// Build Featured Sub-Sections. *
// ********************************
foreach($featuredArticlesArray as $subsectionSlug => $articleArray){
// Start of Sub-Section Box.
echo "<div class='boxSubSection'>
<h3>{$featuredArticlesArray[$subsectionSlug]['subsectionName'][0]}</h3>";
// ****************************
// Build Featured Articles. *
// ****************************
// Build Featured-Article.
echo "<div class='subFeatured'>
<a href=''>
{$featuredArticlesArray[$subsectionSlug]['articleImage'][0]}
<h4>{$featuredArticlesArray[$subsectionSlug]['articleHeading'][0]}</h4>
</a>
{$featuredArticlesArray[$subsectionSlug]['articleSummary'][0]}
</div>";
// ********************************
// Find Articles in Sub-Section. *
// ********************************
// check IF there are subArticles
if (array_key_exists(1,$featuredArticlesArray[$subsectionSlug]['articleSlug']){
// Start of Featured-Links.
echo "<div class='subArticles'>
<ul>";
//Set limit for number of links shown in Loop
for($a=1;$a<=5;$a++){
//check for subArticle key and show if found
if (array_key_exists($a,$featuredArticlesArray[$subsectionSlug]['articleSlug']){
echo "<li>
<a href='/{$featuredArticlesArray[$subsectionSlug]['articleSlug'][$a]}/{$featuredArticlesArray[$subsectionSlug]['articleSubsectionSlug'][$a]}/{$featuredArticlesArray[$subsectionSlug]['articleSlug'][$a]}'>{$featuredArticlesArray[$subsectionSlug]['articleHeading'][$a]}</a>
</li>";
}
}
// End Featured-Links
echo "</ul>
</div>";
}//End check IF there are subArticles
// End of Sub-Section Box.
echo "</div>";
}//End of BUILD FEATURED SUB-SECTIONS
?>
You may of course shorten the name of the array to anything you wish, e.g. $AA for Articles Array etc.
Not at all! Inside a div you can have a tree, a list or anything else you want but the place to build that content is not in the view section of the script. If a div happens to have subsections, you create those in the data part of the script. And inside those sub-sections you can have sub-sub sections and inside those, sub-sub-sub sections as deep as you want or need. The object is to separate data and view so as not to build a pasta factory, a.k.a. spaghetti code.
You can iterate as in foreach(), or you can use recursive functions, or you can use OOP to create the data content and when you get to the view part of the code you simply echo is as in
<div><?php echo $myData; ?></div>
3.) If I rearrange anything, my code will likely be different, so what is your point?!
My point is that you need to get the design right, the actual code, as long as it works, makes little difference. The important issue is the architecture. Once you get the structure right, everything tends to fall in place as if by magic. Thatās my point.
Absolutely catianccs there are many ways to build this data for display.
Direct build without array might go something like this. (not tested of course)
<?php
$sectionSlug = $_GET['section'];
$sectionName = getSectionName($dbc, $sectionSlug);
$dimensionSlug = 'featured-' . $sectionSlug;
// Build Featured Sub-Sections.
$myData = "";
// 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
}
// 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);
$number_of_articles = mysqli_stmt_num_rows($stmt2);
// Build Featured-Articles Array.
$n=1;
while (mysqli_stmt_fetch($stmt2)){
// ********************************
// Build Featured Sub-Sections. *
// ********************************
// Start of Sub-Section Box.
if ($n==1){
$myData .= "<div class='boxSubSection'>
<h3>$subsectionName</h3>";
// ****************************
// Build Featured Articles. *
// ****************************
// Build Featured-Article.
$myData .= "<div class='subFeatured'>
<a href=''>
$articleImage
<h4>$articleHeading</h4>
</a>
$articleSummary
</div>";
}
// ********************************
// Find Articles in Sub-Section. *
// ********************************
// check IF there are subArticles
if ($number_of_articles>1){
if ($n==2){
// Start of Featured-Links.
$myData .= "<div class='subArticles'>
<ul>";
}
//set range of links shown
if ($n>=2 && $n<=6){
$myData .= "<li>
<a href='/$sectionSlug/$articleSubsectionSlug/$articleSlug'>$articleHeading</a>
</li>";
}
// End Featured-Links
$myData .= "</ul>
</div>";
}
}//End check IF there are subArticles
// End of Sub-Section Box.
$myData .= "</div>";
$n++;
}//End of query loop
}else{
// Featured Sub-Sections Not Found.
$myData .= "Featured Sub-Sections Not Found.";
}
?>
Looks like Iām missing the first query loop, which I assume needs to wrap around second query. Again without being able to test this itās hard to know if itās right.
<?php
$sectionSlug = $_GET['section'];
$sectionName = getSectionName($dbc, $sectionSlug);
$dimensionSlug = 'featured-' . $sectionSlug;
// Build Featured Sub-Sections.
$myData = "";
// 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 loop...
while (mysqli_stmt_fetch($stmt1)){
// 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);
$number_of_articles = mysqli_stmt_num_rows($stmt2);
// Build Featured-Articles Array.
$n=1;
while (mysqli_stmt_fetch($stmt2)){
// ********************************
// Build Featured Sub-Sections. *
// ********************************
// Start of Sub-Section Box.
if ($n==1){
$myData .= "<div class='boxSubSection'>
<h3>$subsectionName</h3>";
// ****************************
// Build Featured Articles. *
// ****************************
// Build Featured-Article.
$myData .= "<div class='subFeatured'>
<a href=''>
$articleImage
<h4>$articleHeading</h4>
</a>
$articleSummary
</div>";
}
// ********************************
// Find Articles in Sub-Section. *
// ********************************
// check IF there are subArticles
if ($number_of_articles>1){
if ($n==2){
// Start of Featured-Links.
$myData .= "<div class='subArticles'>
<ul>";
}
//set range of links shown
if ($n>=2 && $n<=6){
$myData .= "<li>
<a href='/$sectionSlug/$articleSubsectionSlug/$articleSlug'>$articleHeading</a>
</li>";
}
// End Featured-Links
$myData .= "</ul>
</div>";
}
}//End check IF there are subArticles
// End of Sub-Section Box.
$myData .= "</div>";
$n++;
}//End of query2 loop
}else{
// Featured Sub-Sections Not Found.
$myData .= "Featured Sub-Sections Not Found.";
}
}//End of query1 loop
}else{
// Error-Handling
$myData .= "Section Not Found.";
}
?>
Drummin:
If it were my design I would have one data routine for each section of the page. Some sections would have simple sql queries, some complex one and others might have multiple queries. It all depends on how the data is organized in the database and how it needs to be transformed for display. You canāt decide that until you know both structures, source and destination. But as I said, the actual code is secondary to the overall design. Maybe all these data routines need to be inside a general sql query.
What I would insist on is making the sql queries do as much of the work as possible to organize the data to simplify the php code that does the final transformation.
Iām not exactly getting the answers I was hoping for, so let me try againā¦
Currently, my second query finds all Articles for all Sub-Sections.
Next, I build an array.
Finally, in the HTML portion of my script, I loop through all Articles in the array, and just display those that apply to the current Sub-Section.
Is taking that approach right or wrong?
It seems to me, that I should already have the proper Articles for the current Sub-Section, and just loop through those and display them? :-/ (But like I have said throughout this thread, I donāt see how to do that.)
Sincerely,
Debbie
If you use the parent category as the primary key in your array building (like I showed in the example with array building) then when you loop under each category you would include this key so only those belonging to parent are shown.
I do see however that the first WHILE loop is missing from that example, which should wrap around the second query.
while (mysqli_stmt_fetch($stmt1)){
<?php
$sectionSlug = $_GET['section'];
$sectionName = getSectionName($dbc, $sectionSlug);
$dimensionSlug = 'featured-' . $sectionSlug;
// Build Featured Sub-Sections.
$featuredArticlesArray = array();
// 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 loop...
while (mysqli_stmt_fetch($stmt1)){
// 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.
while (mysqli_stmt_fetch($stmt2)){
$featuredArticlesArray[$subsectionSlug]['subsectionName'][] = $subsectionName;
$featuredArticlesArray[$subsectionSlug]['articleSlug'][] = $articleSlug;
$featuredArticlesArray[$subsectionSlug]['articleHeading'][] = $articleHeading;
$featuredArticlesArray[$subsectionSlug]['articleSummary'][] = $articleSummary;
$featuredArticlesArray[$subsectionSlug]['articleImage'][] = $articleImage;
}//End of query2 loop
}else{
// Featured Sub-Sections Not Found.
}
}//End of query1 loop
}else{
// Error-Handling
}
?>
Note: The second query should be grabbing articles related to first query.
Drummin,
I have no clue how to read thisā¦
$featuredArticlesArray[$subsectionSlug]['subsectionName'][] = $subsectionName;
Debbie
Debbie,
Quick question for you⦠when you loop through the results of your 2nd query, if itās the first time through you create your featured article for that particular subsection, right? So, is it correct to assume that for the rest of the results for that subsection, articleImage, articleHeading and articleSummary will be empty (or null)?
$featuredArticlesArray[$subsectionSlug][āsubsectionNameā] = $subsectionName;
$featuredArticlesArray == array name. You can change this to any name you wish to shorten.
[$subsectionSlug] == SHOULD BE the primary key thatās defined from first query. All results from second query should be related to this key.
[āsubsectionNameā] == subsectionName key
== open key. First value added under this will have a key of ZERO, second ONE etc.
$subsectionName; == The matching value you wish to place under the [āsubsectionNameā] key.
Itās Debbieās question to answer but I would assume that each article would have an image, heading and summary and though she may not wish to display these in this case, the values should be there.
Correct.
So, is it correct to assume that for the rest of the results for that subsection, articleImage, articleHeading and articleSummary will be empty (or null)?
No.
Each Article has a Slug, Heading, Summary, and Image (path).
To avoid yet another query, I decided to bring over all of that data - not a whole lot! - and then Iād have it for the 1st record, and could disregard it for the other records.
Make sense?
Debbie
The first thing of course is to check if the array is being built as expected with articles are falling under the correct primary key. Use print to view.
<?php
echo "<pre>";
print_r($featuredArticlesArray);
echo "</pre>";
?>
The direct build of display #27 is still an option instead of this whole array building.
I appreciate you guys trying to help, but I really struggle with Arrays in general, and doing this online is even trickier. (We need a real-time white board, or you guys need to come over to my place!!)
[ot]The reason why Arrays are so hard for me to follow, is that I was raised in the Database World, were ONE RECORD can have MANY COLUMNS, and arrays throw that idea out the window!!
(Itās all that damn nesting that drives me crazy, even when I use var_dump to try and visualize thingsā¦)
[/ot]
What I do know, is that all Arrays have a KEY and a VALUE. Period.
A KEY must always be a Scalar.
A VALUE can be a Scalar or another Array.
Arrays can be āIndexedā and have Key-Value Pairs likeā¦
0 => Debbie
1 => Drummin
2 => Fretburner
Arrays can also be āAssociativeā and have Key-Value Pairs likeā¦
Debbie => Arizona
Drummin => Mars
Fretburner => The Moon
Here is how I set up my Array which is one Array nested inside anotherā¦
// ********************************
// 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
And here is how I access values from itā¦
// ****************************
// 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>";
I realize you are trying to add another dimension to the Array, but it seems like you have one too many levels with this codeā¦
// Build Featured-Articles Array.
while (mysqli_stmt_fetch($stmt2)){
$featuredArticlesArray[$subsectionSlug]['subsectionName'][] = $subsectionName;
$featuredArticlesArray[$subsectionSlug]['articleSlug'][] = $articleSlug;
$featuredArticlesArray[$subsectionSlug]['articleHeading'][] = $articleHeading;
$featuredArticlesArray[$subsectionSlug]['articleSummary'][] = $articleSummary;
$featuredArticlesArray[$subsectionSlug]['articleImage'][] = $articleImage;
}//End of query2 loop
Whatās up with that last [ ]??
Follow me??
Debbie
OK, so Iāve re-read your previous posts and had a go at trying to answer these questions you had:
Hereās what Iāve come up with:
$sectionSlug = $_GET['section'];
$sectionName = getSectionName($dbc, $sectionSlug);
$dimensionSlug = 'featured-' . $sectionSlug;
$subsectionSlug = '';
// Queries
$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";
$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 = ?
AND ap.ds_subsection_slug = ?
ORDER BY ap.ds_subsection_slug, RAND()";
// Prepare statement 1
$stmt1 = mysqli_prepare($dbc, $q1);
mysqli_stmt_bind_param($stmt1, 'ss', $sectionSlug, $dimensionSlug);
// Prepare statement 2
$stmt2 = mysqli_prepare($dbc, $q2);
mysqli_stmt_bind_param($stmt2, 'ss', $sectionSlug, $dimensionSlug, $subsectionSlug);
// Execute statement 1
mysqli_stmt_execute($stmt1);
mysqli_stmt_store_result($stmt1);
// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt1) > 0) {
// Bind result-set to variables.
mysqli_stmt_bind_result($stmt1, $subsectionSlug, $subsectionName);
echo "<!-- FEATURED SUBSECTIONS --> "
echo "<div id='boxFeaturedSubsections'> "
echo " <h2>Featured</h2>"
// ********************************
// Build Featured Sub-Sections. *
// ********************************
while (mysqli_stmt_fetch($stmt1)){
// Start of Sub-Section Box.
echo "<div class='boxSubSection'>
<h3>$subsectionName</h3>";
// Execute query.
mysqli_stmt_execute($stmt2);
mysqli_stmt_store_result($stmt2);
// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt2) > 0) {
mysqli_stmt_bind_result($stmt2, $articleSubsectionSlug, $articleSlug, $articleHeading,
$articleSummary, $articleImage);
// Build Featured-Articles
while (mysqli_stmt_fetch($stmt2)) {
$a = 1;
if ($a == 1) {
// Build Featured-Article.
echo "<div class='subFeatured'>
<a href=''>
$articleImage
<h4>$articleHeading</h4>
</a>
$articleSummary
</div>";
} else {
if ($a == 2){
// Start of Featured-Links.
echo "<div class='subArticles'>
<ul>";
}
echo " <li>
<a href='/$sectionSlug/$articleSubsectionSlug/$articleSlug'>$articleHeading</a>
</li>";
}
$a = $a + 1;
}
// End Featured-Links
echo " </ul>
</div>";
// End of Sub-Section Box.
echo "</div>";
}//End of BUILD FEATURED SUB-SECTIONS
} else {
// Featured Sub-Sections Not Found.
}
} else {
// Error-Handling
}
So in answer to your questions:
- Iāve removed the array to try and simplify things.
- The way Iāve changed the code should query the DB for each subsection individually as it generates the HTML⦠Iāve had a go at altering your q2 query to do this, but as I donāt fully understand your data model you may have to tweak this yourself.
- I couldnāt think of a different way to do it, so I think youāll just have to live with a couple if/elseās.
- Now that the array is gone, Iāve shortened the variable names, so they should be a little more readable.
- Iāve removed most of the comments for brevity/clarity - and it should be a little easier to read now that the code has been reorganised a little.
Let me know if it works OK - I apologise for any bugs, but itās hard to test this kind of thing on my own machine.
If you look at your example
Debbie => Arizona
Drummin => Mars
Fretburner => The Moon
Letās say these keys are from your first query āDebbieā, āDrumminā and āFretburnerā. These are the primary keys in your array and the also the identifying variable in your second query. These primary keys are added to the array like this.
$featuredArticlesArray[āDebbieā]
$featuredArticlesArray[āDrumminā]
$featuredArticlesArray[āFretburnerā]
So the first loop is going to find Debbie and second query will look for all articles from Debbie and will add each key => value par under Debbie.
For example sake, article names will be DOG, CAT and MOUSE. The Result will be
$featuredArticlesArray[āDebbieā][āsubsectionNameā][0]=>DOG
$featuredArticlesArray[āDebbieā][āsubsectionNameā][1]=>CAT
$featuredArticlesArray[āDebbieā][āsubsectionNameā][2]=>MOUSE
Once all articles from Debbie are found the second loop starts and finds all articles by Drummin and this process repeats.
Now for displaying these results the primary key would be the section āDebbieā.
The displayed article has the key of zero [0] and any other results [1],[2] etc are shown as links.