I would like to create a Wordpress archive blog template using Bootstrap.
The first row should have the first post as 8 columns and the second post as 4 columns.
The 2nd row and continuing rows should have the posts as 4 columns.
I believe a php counter would enable this template. Does someone know how the code should be written?
Example Template:
Start by getting all the posts with get_posts() to get your posts, which will return an array of WP_Post objects. I'd then loop through the posts like so:
// Get all our posts, and start the counter
$postNumber = 0;
$args = array(
'posts_per_page' => 8
);
$posts = get_posts($args);
// Loop through each of our posts
foreach ($posts as $post) {
// If we're at the first post, or just before a post number that is divisible by three then we start a new row
if (($postNumber == 0) || (($postNumber+1) % 3 == 0)) {
echo '<div class="row">';
}
// Choose the post class based on what number post we are
$postClass = '';
if ($postNumber == 0) {
$postClass .= "col-md-8";
} else {
$postClass .= "col-md-4";
}
echo '<div class="'.$postClass.'">';
// Print the post data...
echo $postNumber. " " . $postClass;
echo "</div>";
// If we are at the second post, or we're just after a post number divisible by three we are at the end of the row
if (($postNumber == 1) || (($postNumber-1) % 3 == 0)) {
echo '</div>'; // close row tag
}
$postNumber++; // Increment counter
}
This will give you some output like this:
You'll obviously need to modify this based on your template but this should give you a nice starting point.
I'm trying to figure out how to do the totalCount inside an if of a while loop
but so far are not successfull in doing so
How can i reach my goal with this code
$rowCount = 1;
while ($clientrows = $statement->fetch()) {
// should return 1000 rows
if ($clientrows['company'] != $current_client) {
echo $rowCount++;
$rowCount++;
// return clientX (one time)
}
if ($clientrows['time'] != NULL ) {
// returns X rows of clientX(from the previews count
// could be any number but lets say here it should be 100 rows
echo $rowCount++;
$rowCount++;
// i need the total counts inside this IF($clientrows['time'] != NULL )
}
echo $rowCount++; // doesn't show anything
// create here the IF total count of previews IF function
// if($totalCounts) {
// echo ' Do something here ';
// }
}
echo $rowCount++; //shows the total rows of while loop while ($clientrows = $statement->fetch())
if i put the count++ right after the IF function it doesn't show anything if i do it at the end then it counts the total of everything
EDIT
Number of counts returns all the rows of my while loop of while ($clientrows = $statement->fetch() (that is about 1000)
what i need to achieve is to get the total rows of this IF statment
if ($clientrows['time'] != NULL ) { }
Which could be 100 or 101 or 200 of the 1000 rows
Help?
"++" is incrementation. It will add + 1 into your variable. It's equal as $a = $a + 1;
Intead of
echo $rowCount++;
$rowCount++;
write
echo $rowCount;
$rowCount++;
Edit.
I just read your comment in script
//shows the total rows of while loop while ($clientrows = $statement->fetch())
Can not you just simply use $statment->num_rows()?
When you do echo $rowCount++; you increment the $rowCount even if you only echo it.
I am creating a questionnaire for a client that requires the questions to be organized by 3 layers of levels. I've successfully created the U.I. however I've been trying for the last 3 hours to pull data from a database in such a way that everything loads in the right place. The database is organized like so by the client so I have no control over it:
id description parentId
1 Level 1 0
2 Level 2 0
3 Level 1a 1
4 Level 1b 1
5 Level 1a1 3
I have found a similar question to mine on the site but when I attempted it's solution I got the following on repeat infinetly:
Code:
function makeList($par_id = 0) {
//your sql code here
$result = mysql_query("SELECT * FROM pB_test WHERE parentId = $par_id");
$pages = mysql_fetch_array( $result );
if (count($pages)) {
echo '<ul>';
foreach ($pages as $page) {
echo '<li>', $page['description'];
makeList($page['parentId']);
echo '</li>';
}
echo '</ul>';
}
}
makeList();
Output:
1
3
5
5
l
l
3
5
5
l
l
3
5
5
l
l
3
5
5
l
l
Does anyone know how to fix this and what the issue is exactly? Cheers
it's not good to call mysql server and fetch result each time
what if you have over 100 rows? or 200+
use this to query only once:
$result = mysql_query("SELECT * FROM test");
$arrs = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$arrs[] = $row;
}
function build_tree($arrs, $parent_id=0, $level=0) {
foreach ($arrs as $arr) {
if ($arr['parent_id'] == $parent_id) {
echo str_repeat("-", $level)." ".$arr['name']."<br />";
build_tree($arrs, $arr['id'], $level+1);
}
}
}
build_tree($arrs);
common example for table
id name parent_id
Do this recursivly:
function printChildQuestions($parentid) {
$sql="SELECT * FROM pB_test WHERE parentID=$parentid";
$result=mysql_query($sql);
$i=0;
while (true) {
$row=mysql_fetch_array($result);
if (!$row) break;
if ($i==0) echo "<ul>";
$i=1;
echo '<li>'.$row['id'].' '.$row['description'].' '.$row['parentId'].'</li>';
printChildQuestions($row['id']);
}
if ($i>0) echo '</ul>';
}
printChildQuestions(0);
I echo results in DESC order from MySQL table with while loop. I've pagination system implemented already and its size is 9 records per page. The problem is, that if I do:
// ECHO CSS BREAK
if($row['id'] % 3 == 0){
echo '<li class="clear"></li>';
}
// SHOW VIDEOS
while($row = mysql_fetch_array($result)){
echo '<li>...echo code...</li>';
// problem = implement that echo css break in ASC order
}
Use a loop variable, e.g.
$i = 0;
Then instead of
if ($row['id'] % 3 == 0) {
do
if (++$i % 3 === 0) {
This makes sure it always happens are the third [sixth, ninth, ...] time.
You may want to get arbitrary rows from the database at another point in time, or shuffle the results -- relying on row IDs is not a good idea.
Is this what you are looking to implement?
// SHOW VIDEOS
while($row = mysql_fetch_array($result)){
if($row['id'] % 3 == 0){
echo '<li>...echo code...</li>';
echo '<li class="clear"></li>';
} else {
echo '<li>...echo code...</li>';
}
}
Have a webpage that will be viewed by mainly IE users, so CSS3 is out of the question.
I want it to list like:
A D G
B E H
C F I
Here is the function that currently lists like:
A B C
D E F
G H I
function listPhoneExtensions($group,$group_title) {
$adldap = new adLDAP();
$group_membership = $adldap->group_members(strtoupper($group),FALSE);
sort($group_membership);
print "
<a name=\"".strtolower($group_title)."\"></a>
<h2>".$group_title."</h2>
<ul class=\"phone-extensions\">";
foreach ($group_membership as $i => $username) {
$userinfo = $adldap->user_info($username, array("givenname","sn","telephonenumber"));
$displayname = "<span class=\"name\">".substr($userinfo[0]["sn"][0],0,9).", ".substr($userinfo[0]["givenname"][0],0,9)."</span><span class=\"ext\">".$userinfo[0]["telephonenumber"][0]."</span>";
if($userinfo[0]["sn"][0] != "" && $userinfo[0]["givenname"][0] != "" && $userinfo[0]["telephonenumber"][0] != "") {
print "<li>".$displayname."</li>";
}
}
print "</ul><p class=\"clear-both\">↑ top</p>";
}
Example rendered html:
<ul class="phone-extensions">
<li><span class="name">Barry Bonds</span><span class="ext">8281</span></li>
<li><span class="name">Gerald Clark</span><span class="ext">8211</span></li>
<li><span class="name">Juan Dixon</span><span class="ext">8282</span></li>
<li><span class="name">Omar Ebbs</span><span class="ext">8252</span></li>
<li><span class="name">Freddie Flank</span><span class="ext">2281</span></li>
<li><span class="name">Jerry Gilmore</span><span class="ext">4231</span></li>
<li><span class="name">Kim Moore</span><span class="ext">5767</span></li>
<li><span class="name">Barry Bonds</span><span class="ext">8281</span></li>
<li><span class="name">Gerald Clark</span><span class="ext">8211</span></li>
<li><span class="name">Juan Dixon</span><span class="ext">8282</span></li>
<li><span class="name">Omar Ebbs</span><span class="ext">8252</span></li>
<li><span class="name">Freddie Flank</span><span class="ext">2281</span></li>
<li><span class="name">Jerry Gilmore</span><span class="ext">4231</span></li>
<li><span class="name">Kim Moore</span><span class="ext">5767</span></li>
<li><span class="name">Barry Bonds</span><span class="ext">8281</span></li>
<li><span class="name">Gerald Clark</span><span class="ext">8211</span></li>
<li><span class="name">Juan Dixon</span><span class="ext">8282</span></li>
<li><span class="name">Omar Ebbs</span><span class="ext">8252</span></li>
<li><span class="name">Freddie Flank</span><span class="ext">2281</span></li>
<li><span class="name">Jerry Gilmore</span><span class="ext">4231</span></li>
<li><span class="name">Kim Moore</span><span class="ext">5767</span></li>
</ul>
Any help is appreciated to getting it to list alpha vertically.
I would load the critical data into an array so you can count them and step through them in whatever order you want. Then use an algorithm like this to get them in the right order:
$items = BuildItemArray(); // Get the values into an array.
$columnCount = 5;
$itemCount = count($items);
$rowCount = ceil($itemCount / $columnCount);
for ($i = 0; $i < $rowCount * $columnCount; $i++)
{
$index = ($i % $columnCount) * $rowCount + floor($i / $columnCount);
if ($index < $itemCount)
{
DisplayItem($items[$index]);
}
else
{
DisplayBlank();
}
}
I think that should work, but I haven't tested it.
I am posting my answer to this old question for the following reasons:
My answer is more general and easy for others to adapt.
I didn't want an overly complex solution.
My array was associative, with a string key. BTW, my solution will work for both associative and indexed arrays.
Actually, the solution I came up with was pretty simple--use multiple tags with style="float:left", inside of a giant table. While I was sceptical that having multiple tbody tags in a single table would pass HTML validation, it in fact did pass without errors.
Note the following
$numCols is your desired number of columns.
Since we are floating items, you may need to set the width and min-width of parent elements and/or add some <br style="clear: both" />, based on your situation.
for alternative sorting methods, see http://php.net/manual/en/array.sorting.php
Here's my full answer:
function sortVertically( $data = array() )
{
/* PREPARE data for printing */
ksort( $data ); // Sort array by key.
$numCols = 5; // Desired number of columns
$numCells = is_array($data) ? count($data) : 1 ;
$numRows = ceil($numCells / $numCols);
$extraCells = $numCells % $numCols; // Store num of tbody's with extra cell
$i = 0; // iterator
$cCell = 0; // num of Cells printed
$output = NULL; // initialize
/* START table printing */
$output .= '<div>';
$output .= '<table>';
foreach( $data as $key => $value )
{
if( $i % $numRows === 0 ) // Start a new tbody
{
if( $i !== 0 ) // Close prev tbody
{
$extraCells--;
if ($extraCells === 0 )
{
$numRows--; // No more tbody's with an extra cell
$extraCells--; // Avoid re-reducing numRows
}
$output .= '</tbody>';
}
$output .= '<tbody style="float: left;">';
$i = 0; // Reset iterator to 0
}
$output .= '<tr>';
$output .= '<th>'.$key.'</th>';
$output .= '<td>'.$value.'</td>';
$output .= '</tr>';
$cCell++; // increase cells printed count
if($cCell == $numCells){ // last cell, close tbody
$output .= '</tbody>';
}
$i++;
}
/* FINISH table printing */
$output .= '</table>';
$output .= '</div>';
return $output;
}
I hope that this answer is useful to you someday.
The title states 5 columns, but your example shows 3. I'll assume 3.
After storing your data in an array, and sorting them, do the following:
For 3 columns, you want position 0,3,6 on the same row. The next row will have increments of 1 of each of those values. So, 1,4,7. The next row will be 2,5,8.
Therefore, you can change your for loop to hold 3 values initially. 0,3,6, and then increment each, and create the next row.
Here is what worked for me. Note that the columns are reset based on the rowCount after it is discovered on the line I labeled(// Added line). You can see I am using a screenWidth and itemWidth to compute my number of columns.
A problem occurs when the number of empty items is greater than the number of rows. If I have 8 columns and 17 records I get 3 rows(Ceiling(17 / 8) = 3). This is a problem (3 * 8) - 17 = 7 empty records. And this is what happens:
RECORD RECORD RECORD RECORD RECORD RECORD ------ ------
RECORD RECORD RECORD RECORD RECORD RECORD ------ ------
RECORD RECORD RECORD RECORD RECORD ------ ------ ------
Since rows are 3 then the line of code that I added fixes things:
columnCount = Math.Ceiling(itemCount / rowCount);
columnCount = 17 / 3 Ceiling = 6(columns) so my layout renders as follows (my client works out the widths).
RECORD RECORD RECORD RECORD RECORD RECORD
RECORD RECORD RECORD RECORD RECORD RECORD
RECORD RECORD RECORD RECORD RECORD ------
Hopefully that helps out anyone who was facing my issue.
private List<CompanyCourseViewModel> Reorder(List<CompanyCourseViewModel> courses, Decimal width, Decimal itemWidth)
{
var list = new List<CompanyCourseViewModel>();
var columnCount = Math.Floor(width / itemWidth);
var itemCount = courses.Count();
var rowCount = Math.Ceiling(itemCount / columnCount);
columnCount = Math.Ceiling(itemCount / rowCount); // Added line.
for (var i = 0; i < rowCount * columnCount; i++)
{
var index = (int) ((i%columnCount) * rowCount + Math.Floor(i/columnCount));
if (index < itemCount)
{
courses[index].NumColumns = (int) columnCount;
list.Add(courses[index]);
}
else
{
list.Add(new CompanyCourseViewModel()
{
Id = -Math.Abs(i - courses.Count()),
Title = "----",
NumColumns = (int)columnCount
});
}
}
return list;
}