I am using osclass classified script, I want to get item id of the last item.
so far i tried following code but it do not work
$lastItem[] = View::newInstance()->_get('search_end');
echo $lastItem[0]['pk_i_id'];
anybody tell me whats wrong with it, or guide me to the right direction.
I believe you can use the latest items.
if (osc_has_latest_items()):
echo osc_item_id();
endif;
It uses the ::getLastestItems() method from the search model object that defaults to descending order. So the first item in the latest items list is actually the most recent.
However be advised that it will overwrite the 'standard' item loop, so don't use it within a while (osc_has_items()): for example.
Related
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.
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?
I have a concrete5 site already using an autonav block in the global header for the main site navigation.
I now need to add a secondary navigation to the header containing different, less important links.
These links aren't necessarily to be shown in sitemap order, but I would prefer to use an auto-nav block if possible to do so.
Can anyone give me any tips on how to achieve this?
Can I get there somehow using Aliases on the sitemap?
Are you looking at using Bread Crumbs? We also use Mega Menu.. There are quite a few menuing systems from concrete5.org. Note: Mega Menu is not free, but is worth it.. The other thing is that sometimes the "File Tree" and "File Sets" may have to be re-ordered.. While in the file Tree, choose the option to update cache..
You should use a page attribute. You could use a checkbox for instance, call it secondary and if checked make your secondary nav to show it.
You can use a page list and an boolean attribute(for example: display_in_second_nav)
Here is an example:
$pl = new PageList();
$pl->filterByAttribute('display_in_second_nav');
$pages = $pl->get();
Also, you can sort results:
$pl->sortByRelevance()
Orders by index score descending. Only available when searching by keywords.
$pl->sortByDisplayOrder()
Orders by sitemap display order ascending.
$pl->sortByDisplayOrderDescending()
Orders by sitemap display order descending.
$pl->sortByPublicDate()
Orders by public date ascending.
$pl->sortByPublicDateDescending()
Orders by public date descending.
$pl->sortByName()
Orders by page name ascending.
$pl->sortByNameDescending()
Orders by name descending.
I am trying to implement a way to sort elements in a list based on the order they were added from oldest to newest. This would be easy to do in MySQL using an ORDER BY on a created DATETIME. The problem is I then want to be able to move elements up and down the list and have that order saved in the database, but also make sure that new elements get put on the end of the list.
I have thought of using an INT index and just increment that for each new item that is added to the list. Then when an existing item is moved up or down in the list, swap the index numbers. Does this sound like the best way to achieve this result? If not, would anyone be able to provide some insight of a better way. Thanks
So I ended up solving this by using an INT field in the MySQL table called order. When new items are added, the order field is just incremented from the previous largest order in the table. When items are moved up or down in order, all other items orders are shifted up or down accordingly so that the order numbers are always continuous. On delete, orders are also shifted down.
I am getting a collection of categories using
foreach(Mage::getModel('catalog/category')->getCollection() as $category)
For each of these categories, I need to retrieve the sort order. How do I do this?
The only function I can find that looks relevant in the category class is getDefaultSortBy(), which always returns news_from_date, which is neither the default sort order (position) or the selected sort order (price), so I don't know where that's getting its value from. I also noticed that if, in the magento backend, I change the list of available sort orders, the function getAvailableSortByOptions still always returns the same array. From these two facts, I conclude that the category functions must be accessing some sort of cross-category global settings, which is useless to me.
I want the specific sort order chosen for each specific category. Is there any way to retrieve this? Or do I need to write my own SQL? In that case, which table do I need to query?
I am using magento enterprise ver. 1.11.1.0
Found the answer. Naturally, moments after I posted the question!
The problem is that Mage::getModel('catalog/category')->getCollection() does NOT automatically load all of the category attributes. You have to indicate which ones to retrieve manually. So, I need to replace this:
foreach(Mage::getModel('catalog/category')->getCollection() as $category)
With this:
foreach(
Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('default_sort_by')
->addAttributeToSelect('available_sort_by')
as $category
)
I am trying to create a pagination with just next and previous links instead of 1,2,3..etc.
So I have say item 1,2,3,4,5 in the database and each item is on one page.
If I am on item 2 page I would like a previous link for item 1 page and a next link for item 3 page...etc
So far I got up to retrieving all the items in the database and created a loop to spit out all the URL in each item. But I don't quite know the best way to strip off all the items except the adjacent items. This is the loop I have so far.
foreach ($item_url as $url) :
echo $url['url'];
endforeach;
This spits out all URL...But how do i proceed to list only the adjacent items and not count the current item I am on...I tried using next($url); to move the pointer if it matches the current item but it didn't work...
Thanks, guys!
Here's a neat resource for paging tabular data without the headache of writing your own code or the need for any PHP at all. Throw all the results into a table (or output them via JSON if you are so inclined) Now, apply the DataTables plugin to the table id and tell it to do pagination. Voila, instant pagination, no brain damage required.