-3

I am learning PHP and have decided to code my own OOP MVC framework. Now, I have realized several times already that it might not be the smartest move but I mean to see this out to the end. And then onwards...

My issue is creating a listing sidebar based on categories and a second based on year-month-postname. I am officially stuck on the first one, let alone the second damn option. I have included some code and description of what I have tried. The lack of OOP info on the net is daunting or maybe it is because I am searching for the wrong thing, I dont know. But the tutorials have not given me any insight as to how to actually do this in a way where my database is in a model file and my class logic is in the class file.

Sorting logic should be like this Array-Object-Propertyname-Value. The value, as I hope is easy to understand in my example below, is the category name eg Javascript, PHP, HTML. By that category i wish to sort my blog posts. But not in the way that requires me to manually input the category names to the code. I want to allow users to enter categories if they so choose. I also wish to display the blog posts inside said category, lets say 5 most recent. But that should not be too hard with a

for($i=0,$i<5,i++)

nested inside whatever solution in the end will work for the category sort.

I have tried MySQLI procedural solutions ranging from multiple google searches and tutorials. Can do it, but dont want to do it procedurally. Tried foreach loop and nesting multiple foreach loops - simply cannot get either the problem of having duplicates based on the shared category name or if trying to group in the SQL query, it simply groups results with same category and then displays only the first one in the group. While loops with mysqli procedural work but with pdo in my case they produce infinite loops, no matter the condition I try to set. So foreach is the way to go I believe. I have read up on loops and array sorting but I've yet to find a solution. I thought of sorting by key because that is what i need but to no applicable solutions. It's easy to display the category names and dates and all that. But with category I always get duplicates. Ive tried some logic where as to assign category names as variables but only to have them all be different variables, meaning still having duplicates or only rewriting the variable with each iteration. Also, array sorts havent worked because I havent gotten any to work with sorting either on property or if converting Objects to a multidimensional array. Granted that may be because I am a beginner and not understood the syntax but I am not going to post them all here I think. If you think an array sorting function will do the trick then perhaps give me an example and I will look into it with some new perspective hopefully.

PDO query :

'SELECT * FROM postTable 
INNER JOIN userTable ON postTable.postUserId = userTable.id 
INNER JOIN postCategories ON postTable.postCategoryId = postCategories.categoryName 
ORDER BY postTable.postDate DESC'

Tried also to add

GROUP BY categoryName

but that resulted in only one entry per category shown when using var_dump. Sidenote - same is when grouping by creation date. Is there another layer added to the array when using group in the SQL command and I missed that in the docs?

PDO returns to view file :

$this->stmt->fetchAll(PDO::FETCH_OBJ);

this all gets passed into an array of

$results

and then that is sent to the php on the view page where the resulting array has this structure with var_dump.

array() {
  [0]=>
  object(stdClass)# () {
   ["categoryName"]=>
    string() "Help"
  }
  [1]=>
  object(stdClass)# () {
   ["categoryName"]=>
    string() "Me"
  }

and so on.

Note - also tried using -

fetchAll(PDO::FETCH_ASSOC);

But ive had similar failures with attempting any sorting or limiting to just one category name displayed but all entries under said category being displayed correctly and not just one per category.

I will be checking back when i finish work tomorrow so in about 20-22 hours from the time of posting. If you need any more info just let me know and I'll post it.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Kristo Leas
  • 50
  • 1
  • 7
  • 5
    You should try short the question - narrow it down to what you have, expected output and what you have tried. Show us the output of the SQL query. Sorting in PHP should be quite easy – dWinder May 01 '19 at 20:19
  • It will take some time to try to recreate all of the solutions but If you think it will help more then I will try tomorrow after work. – Kristo Leas May 01 '19 at 20:29

1 Answers1

1

You can order by multiple columns. Use:

ORDER BY categoryName, postDate DESC

This will keep all the posts in the same category together, and in decreasing date order within each category.

See How can i list has same id data with while loop in PHP? for how you can output the results, showing a heading for each category.

If you just want to get the 5 latest posts in each category, see Using LIMIT within GROUP BY to get N results per group?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Ok everything in your answer is sound and what Im looking for except the How can I list thingy. I have database functions in a db file, database querys in a model file and the page specific functions and array actions in a controller file. The view that displays the actual data and would have this loop in it does not access any of those save for controller and gets passed an array from controller with data. So as to how would I go about not using the while loop or do I need to somehow either move db functions to a file that is accessible to views or write perhaps a function helper for that? – Kristo Leas May 02 '19 at 18:28
  • If you're fetching all the query results into a 2-dimensional array, you can replace the `while ($row = fetch())` loop that processes query results with `foreach ($rows as $row)`, where `$rows` is the array of all results. – Barmar May 02 '19 at 19:11
  • Oh my god... I feel so stupid right now. Id tried the foreach but what I did not do was use the != operator. I tried all sorts of combinations and got infinite loops and array sort function results turned out integers. And it is so simple. Thank you. This is all I needed and more! – Kristo Leas May 02 '19 at 19:26