So I'm wanting to make a way to display the 2nd newest row from my database. So my example (To help better explain it) If i have 10 rows. I want to display only the second newest one. Not the newest one but the one right after it. Or the maybe even the third one. I have my working code below and i see the for each loop. But I'm not sure how to only display the one I'm wanting it to display. I also don't think how i have it set up is the most efficient way of acomplishing this.
I am using concrete5 for this site and the main idea is for composer so i can post a new post and the pull a recent news feed but showing the second post.
Here is my current code: (This shows the first post)
<?php
defined('C5_EXECUTE') or die("Access Denied.");
?>
<div id="blog-index">
<?php
$isFirst = true; //So first item in list can have a different css class (e.g. no top border)
$excerptBlocks = ($controller->truncateSummaries ? 1 : null); //1 is the number of blocks to include in the excerpt
$truncateChars = ($controller->truncateSummaries ? $controller->truncateChars : 255);
$imgHelper = Loader::Helper('image');
foreach ($cArray as $cobj):
$title = $cobj->getCollectionName();
$date = $cobj->getCollectionDatePublic(DATE_APP_GENERIC_MDY_FULL);
$author = $cobj->getVersionObject()->getVersionAuthorUserName();
$link = $nh->getLinkToCollection($cobj);
$firstClass = $isFirst ? 'first-entry' : '';
$entryController = Loader::controller($cobj);
if(method_exists($entryController,'getCommentCountString')) {
$comments = $entryController->getCommentCountString('%s '.t('Comment'), '%s '.t('Comments'));
}
$isFirst = false;
?>
<div class="entry">
<div class="title">
<h3>
<?php echo $title; ?>
</h3>
<h6 class="post-date">
<?php
echo t('Posted by %s on %s',$author,$date);
?>
</h6>
</div>
<div class="excerpt">
<?php
$a = new Area('Main');
$a->disableControls();
if($truncateChars) {
$th = Loader::helper('text');
ob_start();
$a->display($cobj);
$excerpt = ob_get_clean();
echo $th->entities($th->shorten($excerpt,$truncateChars));
} else {
$a->display($cobj);
}
?>
</div>
<div class="ccm-spacer"></div>
<br />
<div class="meta">
<?php echo $comments; ?> <?php echo t('Read the full post'); ?> <i class="icon-chevron-right"></i>
</div>
</div>
<hr class="blog-entry-divider"/>
<?php endforeach; ?>
</div>
To build upon what the others have written as comments:
What you've pasted is a concrete5 view. You'll notice there's no db querying or PageList building in there. For that, you need to look in the controller. (This looks like a page list block view, so the controller will be in / concrete / core / controllers / blocks / page_list.php (on c5.6+).
The concrete5 api code to do what the others have suggested (let mysql handle the skipping) is done within the ->get() call. So, on about line 135:
$pl->get(1, 1);
Remember not to modify the files directly, but to override them the c5 way. There are plenty of tutorials on this on the c5 website.
Related
Making mobile site with Concrete5 and using page list block with custom template. I'm trying to count sub pages using PHP.
<?php foreach ($pages as $page):
// Prepare data for each page being listed...
$title = $th->entities($page->getCollectionName());
$url = $nh->getLinkToCollection($page);
$target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
$target = empty($target) ? '_self' : $target;
$description = $page->getCollectionDescription();
$description = $controller->truncateSummaries ? $th->shorten($description, $controller->truncateChars) : $description;
$description = $th->entities($description);
$mies = 0;
?>
<li class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c" data-theme="c"><div aria-hidden="true" class="ui-btn-inner ui-li"><div class="ui-btn-text"><a target="<?php echo $target ?>" class="ui-link-inherit" href="<?php echo $url ?>">
<h2 class="ui-li-heading"><?php echo $title ?></h2>
<p class="ui-li-desc"><?php echo $description ?></p>
</a>
</div><span class="ui-icon ui-icon-arrow-r ui-icon-shadow"></span><span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo count($mies) ?></span></div></li>
<?php endforeach; ?>
So, probably need to use Count function(or length?), I don't know. If I am editing wrong place please advice if you have any experience in Concrete5 cms.
If you want to show the corresponding page number in the span element in your code:
<span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo $mies; ?></span>
If you want to show the remaining sub-pages, then in the html code snippet above just replace $mies with count($pages) - $mies like:
<span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo count($pages) -$mies; ?></span>
You would first have to initialise $mies before you start the forloop so it should be something of the form:
<?php
$mies = 0;
foreach ($pages as $page):
//Rest of your code and increment $mies with every iteration
$mies ++; //This ensures that you are counting the corresponding pages
?>
If you want to get the count of total number of sub-pages, you just have to echo out $mies outside the for block may be like:
<?php
endforeach;
echo $mies; //Shows you the total number of pages processed inside the for loop.
//or Just find the length of pages array
echo count($pages);
?>
As far as getting the length of array is concerned you could use count or sizeof. I stumbled upon a SO question about using count or sizeof method for finding the length of an array.
This should get you started in the right direction.
You need the parent ID;
$parentId = Page::getCollectionParentId();
Note that Page::getCollectionParentId() gets the current page's parent ID,so you may want to try;
$parentId = $page->getCollectionParentID();
Then create a new PageList to filter with and filter by the parentId;
Loader::model('page_list');
$pl = new PageList();
$pl->filter(false, '(p1.cParentID IN ' . $parentId . ')');
// Get the total
var_dump($pl->getTotal());
This is untested but the theory makes sense.
This is likely a bit simpler.
$pl->getTotal()
$pl is the PageList object that is set in the controller.
Also, these days you can just use the h() method instead of writing out $th->entities()
Edit: I should clarify that you don't need to do a $pl = new PageList() because $pl is already set to the PageList object in the controller and passed to the view.
I am trying to teach myself PHP. I have looked to find out how to make this work.
I have an online portfolio that I am posting my weekly school assignments and their grades when I receive them.
I currently have something that looks like this:
<?php
$page_title = "MATH203 | Journey to my Oasis";
$grade = "A = 185/185";
$course_title = "Applications of Discrete Mathematics";
$course_description = "Math. A fun four letter word. A lot of people have issues with this subject. I like math. I am not proficient by any stretch of the imagination, but I like it. It is challenging. Having this course after programming, I believe, will allow me to think of it in the manner in which it is intended. The professor is also teaches computer science. This should be interesting.";
?>
<?php include("../includes/header.php");?>
<?php include("../includes/navigation.php");?>
<?php include("../includes/classannounce.php");?>
<div id="content">
<div id="content_container">
<div class="one_half">
<h3>Discussion Board Posts</h3>
<h4><ul>
<li>
Week 1
Grade - A
</li>
<li>
Week 2
Not yet graded
</li>
<!--
<li>
Week 3
Grade - A
</li>
<li>
Week 5
Grade - B-
</li>
-->
<li>All links open a new window</li>
</ul></h4>
</div>
</div><!--end content_container-->
</div><!--end content-->
'<?php include("../includes/footer.php");?>
</body>
What I would like to do is have a separate PHP file that I include in the header PHP file so it goes everywhere with me just to cut down on the amount of code.
Currently what I am doing is just hiding the html for the weeks that I haven't come up to yet. I am using the original page as a template and just changing it as I go. I am trying to incorporate PHP more so that I can make changes easier and so that I can try to learn as much as I can. I figure that by calling a function an populating it with the variables needed I can cut down on the code needed and time required.
This separate file would have a function like:
<?php
function assignment()
{
$skydoc = '';
$week_no = '';
$grade_letter = '';
echo '<li><a href="', $skydoc, '" target="_blank">Week ', $week_no,
'</a> Grade - ', $grade_letter, '</li>';
}
?>
I try calling the function an include the variables in it so that it will fill in the appropriate sections.
assignment($skydoc = 'url', $week_no = '2', $grade_letter = '?')
I try that in the same file for testing purposes but all I get in HTML when I try it on my server is this
<li>
Week
Grade -
</li>
Thank you in advance. Any more questions or need clarification, please let me know.
your calling the function a bit incorrectly. function declaration should have the place holders for the variables you are passing into it.
function assignment($skydoc = 'url', $week_no = '2', $grade_letter = '?')
{
echo '<li>
Week ',$week_no,'
Grade - ',$grade_letter,'
</li>';
}
Then when you want to call the function you simply invoke the following
assignment('value', 1, 'A');
if you call assignment as follows then the default values you have placed in the variables will be assinged.
assignment(); // url, 2, ?
close...
function assignment($skydoc = '', $week_no = '', $grade_letter = ''){
echo '<li>
Week '.$week_no.'
Grade - '.$grade_letter.'
</li>';
}
assignment('url', '2', '?');
My website consists of many products that are each contained in a div with the id content block. The link, image, background, description and price are all loaded from a mySQL table. My original plan was to save the below html code as a string and loop over the rows in the mySQL table filling the string I created with php/mySQL values.
I was wondering if I am going about this the right way, or is there a better way to create html code from php variables?
<div id="contentblock" style="background-image:url(images/$BACKGROUND.png);">
<div id="picture"><img src="$IMAGELINK"/></div>
<div id="description"><p>$DESCRIPTION</p></div>
<div id="price"><p class=price>$PRICE</p></div>
</div>
Firstly PHP is a template engine - in my experience template engines that layer ontop of PHP are only good for the simplest of cases and are easily outgrown.
Secondly the original code is as good as any method. At risk of stating the obvious to make it better abstract it into a function;
function output_block($BACKGROUND, $LINK, $IMAGELINK, $DESCRIPTION, $PRICE)
{
echo "<div id='contentblock' style='background-image:url(images/$BACKGROUND.png);'>
<div id='picture'><a href='$LINK'><img src='$IMAGELINK'/></a></div>
<div id='description'><p>$DESCRIPTION</p></div>
<div id='price'><p class=price>$PRICE</p></div>
</div>";
}
If you want to make it much better then adopt a framework, an entire admin config page is show below. All of the HTML glue is provided by the framework - the following code is real, but really to illustrate how a framework can provide a lot of the grunge work for you.
In the example below if I want to edit a single entity I'd change the TableViewEdit into a FormView and provide an instance of an entity rather than an iterable list.
$entity = new CbfConfig(); // Database entity
$page = new AdminWebPage("Site Configuration"); // Page for output
/*
* build the view
*/
$vil = new ViewItemList();
$col = &$vil->add(new ViewItem("description","Description"));
$col->get_output_transform()->allow_edit(false); // this field cannot be editted
$col = &$vil->add(new ViewItem("value","Value"));
$v1 = new TableViewEdit($entity, $vil,"admin_values"); // present as standard editable table
/*
* output the page
*/
$page->begin();
$iterable_list = CbfConfig::site_begin();
$page->add_body($v1->get_output($iterable_list,'admin_config'));
$page->end();
Id just have all my html code outside of php tags, then whereever I need a variable from php do as follows
<div id="description"><p><?php echo $DESCRIPTION; ?></p></div>
You can loop around non php code too. For example
<?php
for($i = 0; $i < 10; $i++) {
?>
<div id="description"><p><?php echo $i; ?></p></div>
<?php
} //end for loop
?>
Obviously this is just an example.
well if im without a template engine for somereason i usually do something like:
function partial($file, $args = array()) {
extract($args);
ob_start();
include($file);
return ob_get_clean();
}
Really, there are 3 ways of doing this. Use whichever is easiest for you in the context that you are using it in.
<?php
while(($row=mysql_fetch_assoc($result))!==false)
{
echo "<div>{$row['fieldName']}</div>";
}
?>
<?php
while(($row=mysql_fetch_assoc($result))!==false)
{
echo '<div>'.$row['fieldName'].'</div>';
}
?>
<?php
while(($row=mysql_fetch_assoc($result))!==false)
{
?>
<div><?= $row['fieldName']; ?></div>
<?php
}
?>
Im developing a website and I am trying to a foreach include for my header which includes my navigation menu (the main topic here).
My code inside the header.php file for the navigation menu is here:
<!-- topmenu -->
<div class="menu-header">
<div class="container">
<ul class="top menu">
<?php
$nav = array("Home","About","Portfolio","Products","Services","Contact");
foreach($nav as $item){
if($item == $title){
echo "<li class='current-menu-ancestor parent'><a href='$item.php'>$item</a></li>";
}else{
echo "<li><a href='$item.php'>$item</a></li>"; }
}
?>
</ul>
</div>
</div>
<!--/ topmenu -->
You may notice that in the code is the condition if($item == $title). In my index.php I have included $title="Home"; which I intended to be taken and used in this if statement.
On my index.php page I have included this with the following code:
<?php
include("header.php");
$title = "Home";
?>
Can anyone tell me where I am going wrong?
Not an answer, but comments aren't suitable for formatted code. You might want to de-duplicate some of your HTML:
$class = '';
if($item == $title) {
$class = ' class"current-menu-ancestor parent"';
}
echo "<li{$class}><a href='$item.php'>$item</a></li>";
Duplicating HTML as you did can lead to maintenance problems later on, if you decide to change something in the menu structure and change only one of the copies of the menu html.
header.php can not look into the future. If you want the variable to be set in the include, you need to set it before:
<?php
$title = "Home";
include("header.php");
?>
So you basically just switched the lines.
Additionally I suggest that you enable error reporting to the highest level when you develop, as it will give you warning on common mistakes that can happen while typing code.
You can do this by adding the following two lines to the top of your script:
error_reporting(~0);
ini_set("display_errors", "1″);
or by changing your PHP configuration.
You need to set $title before including header.php!
i.e.
<?php
$title = "Home";
include("header.php");
?>
What I'm trying to do is to add some HTML tags to my Joomla! module titles. I will need something like this
Some <b>Title</b>
but when I save !Joomla trims the titles and remove all HTML tags.
I've check the administrator/com_content, which I think should be responsible for inserting the database data, but I couldn't find the answer there.
Can anyone help me with this headache?
Check out ../templates/system/html/modules.php
You can style your module structure in HTML.
function modChrome_myCustomModule($module, &$params, &$attribs)
{
$doc =& JFactory::getDocument();
$css = ".otherClass {}";
$css .= ".yourClass {}";
$doc->addStyleDeclaration($css);
?>
<div>
<?php if ($module->showtitle != 0) : ?>
<h1><?php echo $module->title; ?></h1>
<?php endif; ?> // post your title
</div>
<div>
<?php echo $module->content; ?> // post your module content
</div>
<?php
}
Then call your styled module in index.php:
<jdoc:include type="modules" name="right" style="myCustomModule" />
So I found the solutions. It includes both of the previous answers, so I'm putting a new one here with the correct code.
First of all I need to say, that this solution works only for a fixed amount of words (last one, two, etc.) I need only to have the last one, so I will post an example code with one word.
First as SMacFadyen sad I needed to create a new module structure in my template html folder: /templates/system/html/modules.php file.
Note: If you don't want to add this new module styling to all templates, but just on one of them you need to put the module.php in your template's html folder.
The provided by SMacFadyen looks like this:
function modChrome_myCustomModule($module, &$params, &$attribs)
{
$doc =& JFactory::getDocument();
$css = ".otherClass {}";
$css .= ".yourClass {}";
$doc->addStyleDeclaration($css);
?>
<div>
<?php if ($module->showtitle != 0) : ?>
<h1><?php echo $module->title; ?></h1>
<?php endif; ?> // post your title
</div>
<div>
<?php echo $module->content; ?> // post your module content
</div>
<?php
}
Then expired by the comments of Hanny I've added some php code to match the last word of the title and to store it in a new varibale.The code looks like this:
$wrap_tag = 'b';
$html_title = preg_replace("~\W\w+\s*$~", '<'.$wrap_tag.'>'.'\\0'.'</'.$wrap_tag.'>', $module->title);
Note: the $wrap_tag variable stores the tag you want. You can put b, em, u and etc. to have different result.
The last thing was to replace the displayed title, so I've replaced this code:
<h1><?php echo $module->title; ?></h1>
with this one:
<h1><?php echo $html_title; ?></h1>
The final result was this:
function modChrome_myCustomModule($module, &$params, &$attribs)
{
$doc =& JFactory::getDocument();
$css = ".otherClass {}";
$css .= ".yourClass {}";
$wrap_tag = 'b';
$html_title = preg_replace("~\W\w+\s*$~", '<'.$wrap_tag.'>'.'\\0'.'</'.$wrap_tag.'>', $module->title);
$doc->addStyleDeclaration($css);
?>
<div>
<?php if ($module->showtitle != 0) : ?>
<h1><?php echo $html_title; ?></h1>
<?php endif; ?> // post your title
</div>
<div>
<?php echo $module->content; ?> // post your module content
</div>
<?php
}
Thanks to everybody for the help.
The Gantry framework can help you accomplish what you want (1st word styled one way, 2nd word styled another) - but it's a lot of overhead just accomplish that one task you're looking for. Ultimately you'll have to create a template override for your template, and then do some creative editing with php in order to get it to display that way.
There's no quick and easy way to get that done. You'll have to do some php coding on the backend and edit the template (use an override so you don't hack core files). Ultimately you'll probably have to code the php to pull apart the title, and apply formatting to each pulled apart word (or string of words as the case may be) using CSS.
Hope that helps.