I have category table which include 35 category inside. I want them aline into four div with one while loop.
<?php
$count = 0;
$cat = pullcategories();
$catcount = mysqli_num_rows($cat);
$percat = ceil($catcount / 4);
$topcats = pulltopcategories($count, $percat);
//same with pullcategories, just LIMIT $count,$per //
while ($topcats = mysqli_fetch_object($topcats)) {
?>
<div class="col3">
<ul class="list-unstyled list-dashed">
<li>
$topcats->title
</li>
</ul>
</div>
<?php
$count = $count + $percat;
$percat = $percat * 2;
}
?>
$count = 0;
$cat = pullcategories();
$catcount = mysqli_num_rows($cat);
$percat = ceil($catcount / 4);
$topcats = pulltopcategories($count, $percat);
This breaks your lists down in to 4 parts, and you would need to call "$topcats = pulltopcategories($count, $percat);" 4 times to be able to get through all the data.
I would change it to be something like this.
Assuming that "pulltopcategories($count, $percat)" uses count as the limit, and percat as the offset in the SQL this should work.
Please try and let me know - I can fix any mistakes if there are any (my PHP is a little rusty and i dont have data to try this with)
<?php
$count = 0;
$cat = pullcategories();
$catcount = mysqli_num_rows($cat);
$percat = ceil($catcount / 4);
while ($count < catcount){ /* Loop until you have finished all cats */
$topcats = pulltopcategories($count, $percat);
echo "<div class="col3">
<ul class='list-unstyled list-dashed'>";
while ($topcats = mysqli_fetch_object($topcats)) {
echo "<li>
<a href=''>$topcats->title</a>
</li>"
}
echo "</ul>
</div>";
$count = $count + $percat;
}
?>
Related
I need to divided all data into 4 columns. i have used this method but its not coming column property
$stmt1 = $db->prepare($sql1);
$stmt5 = $db->prepare($sql1);
$stmt5->execute();
$rowcount = $stmt5->rowCount();
$pages = ceil($rowcount / 4);
$tempcount = 1;
if ($stmt1->execute(array())) {
while ($row = $stmt1->fetch()) {
?>
<?php if ($tempcount == $pages) { ?>
<div class="column">
<div class="ui bulleted list">
<?php } ?>
<?php
if ($tempcount == $pages) {
$pages = $pages + $pages; ?>
</div>
</div>
<?php }
$tempcount++;
}
} ?>
how i can get this columns after getting count of data i have and add those divs in between.
for an Example 20 data rows 20/4 = 5 after each 5 data, i need start and end to be added
<div class="column">
<div class="ui bulleted list">
</div>
</div>
<div class="column">
<div class="ui bulleted list">
</div>
</div>
// And so on
result will be like this
thank you very much
Here's a proper code with comments
$tempcount = 0;
$pages = ceil($rowcount / 4);
while ($row = $stmt1->fetch()) {
// if result of % (Modulo) is zero - you need to start new column
if ($tempcount % $pages == 0) {?>
<div class="column">
<div class="ui bulleted list">
<?php
}?>
ITEM
<?php
// if result of % (Modulo) is (pages - 1) - you need to close previous column
if ($tempcount % $pages == ($pages - 1)) {?>
</div>
</div>
<?php
}
$tempcount++;
}
// check if you have to close previous column
// because it was not closed in a while loop
if (0 < $rowcount) {
if ($tempcount % $pages != 0) {?>
</div>
</div>
<?php
}
}
Whilst the modulo approach mentioned above will help in these situations, I often find that writing your loops from a different perspective can help in sorting out the situation, and making things more readable.
Rather than looping through every row, and then inserting the columns where you think they need to be. Instead loop every item, but in a controlled count, wrapped by the columns.
I've switched the code to using echo just due to personal preference, you can still use php-breakouts instead if preferred.
Please note this is untested example code, just to illustrate the point:
<?php
$stmt1 = $db->prepare($sql1);
$stmt5 = $db->prepare($sql1);
$stmt5->execute();
$rowcount = $stmt5->rowCount();
$pages = ceil($rowcount / 4);
if ($rowcount) {
$stmt1->execute(array());
do {
$group = '';
$group .= '<div class="column">';
$group .= '<div class="ui bulleted list">';
for ( $i=0; $i<$pages; $i++ ) {
$row = $stmt1->fetch(); if ( !$row ) { break; }
// presumably something would be done with $row here
$group .= '';
}
$group .= '</div>';
$group .= '</div>';
echo $group;
} while ( $row );
}
% modulo will help you PHP operators. While you fetch, use it to create your divider. If using div and if needed, I usually add a tiny div with clear: both to make sure it breaks the line after the 4/8/12... results.
$cell++;
$jumpline = ($cell % 4) ? "" : "<div class=\"spacer\"></div><br />";
$endit = ($cell % 4) ? "" : "</div>";
I am currently working on a search system and I want to display all rows that have 2 column names, problem is, my while loop only displays 1 row when I have 2 of them in the DB. I've been working my head around this for hours but I just can't get it. The only output I get is the last price.
<?php
include_once("inc/head.php");
include_once("inc/db.php");
$leave = $_POST['departure'];
$come = $_POST['destination'];
$leavedate = $_POST['leavedate'];
$comedate = $_POST['comedate'];
$sql = "select * from flights where departure = '$leave' and destination ='$come'";
$result = $db->query($sql);
$row = $result->fetchObject();
$price = $row->price;
$mostflights = $row->departure;
$mostcomings = $row->destination;
?>
<div>
<ul class="flights">
<?php
while($row = $result->fetchObject())
$price = $row->price;
$mostflights = $row->departure;
$mostcomings = $row->destination;
{ ?>
<li>
<h3><?php echo $mostflights; ?></h3>
<h2><?php echo $mostcomings; ?></h2>
<h2><?php echo $price; ?></h2>
</li>
<?php
}
?>
</ul>
</div>
remove this block at 10th line
$row = $result->fetchObject();
$price = $row->price;
$mostflights = $row->departure;
$mostcomings = $row->destination;
I am kinda lost with what I am trying to achieve here, really need some help with the following.
While fetching DB using script provided below, I need to add <div class="clear"></div> after every third occurrence and if it is less than 3, lets say 2 or even one and there is no more after that after that last one.
Here is my script
<?
$template_query = 'SELECT * FROM Files WHERE parentpageID = :id and show_in_category = "1" ORDER BY ID asc';
$res = $db->prepare($template_query);
$res->execute(array(':id' => $current));
$add_rowNum = 0;
while ($info = $res -> fetch()){
$add_rowNum++;
$templateTitle = $info['templateTitle'];
$add_refering_url = $info['referring_url'];
$templ_link = $category_folder.$add_refering_url;
$teaserText = $info['teaserText'];
$path_to_add_images = $image_path.$info['ImagePath'].DS;
$add_img_info = $path_to_add_images.$info['templateImage'];
$add_img_alt && $add_img_title && $templateTitle = $info['templateTitle'];
list($width, $height, $type, $attr) = getimagesize($add_img_info);
$last_class = ($add_rowNum == $res->rowCount()) ? 'frame' : 'frame frame_margin';
print<<<END
<div class="$last_class">
<div class="prod">
<div class="title"><a href="$templ_link">$templateTitle
<img src="$add_img_info" alt="$add_img_alt" $attr title="$add_img_title"></a>
</div>
</div>
<div class="prd">
$teaserText
</div>
</div>
END;
}
?>
your help is highly appreciated
Use below code:
If(($add_rowNum % 3) == 0) {
echo "<div class='clear'></div>";
}
Add this code after the END syntax.
Using with %
if ($add_rowNum % 3 == 1)
echo '<div class="clear"></div>';
Try this. I think this logic will solve this problem!
$total_records = 10;
for($i=1;$i<=$total_records;$i++) {
echo "Hi";
if($i%3 == 0) {
echo "<br/>==================<br/>";
}
if(($i == $total_records) && ($total_records%3 != 0)) {
echo "<br/>==================<br/>";
}
}
I have the code below that is selecting a random set of questions from Wordpress.
<?php
$rows = get_field('step_by_step_test');
$row_count = count($rows);
$rand_rows = array();
$questions = get_field('select_number_of_questions');
for ($i = 0; $i < min($row_count, $questions); $i++) {
$r = rand(0, $row_count - 1);
while (array_search($r, $rand_rows) !== false) {
$r = rand(0, $row_count - 1);
}
$rand_rows[] = $r;
echo $rows[$r]['question'];
}
?>
I want to incorporate a bit of extra code (below), how can I make sure it's selecting the same random question?
<?php if(get_sub_field('answer_options')): ?>
<?php while(has_sub_field('answer_options')): ?>
<?php echo the_sub_field('answer'); ?>
<?php endwhile; ?>
<?php endif; ?>
Why dont you change your approach slightly?
<?php
$rows = get_field('step_by_step_test'); // Get the test
$question_count = get_field('select_number_of_questions'); // Get the number of questions
$rows = shuffle($rows); // Randomize your questions
$rows = array_slice($rows, $question_count); // Now set the array to only contain the number of questions you wanted
foreach ($rows as $row) {
echo $row['question']; // Show the question
if(get_sub_field('answer_options', $row['id'])) {
while(has_sub_field('answer_options', $row['id'])) {
echo the_sub_field('answer');
}
}
}
?>
I made the assumption that you can alter "get_sub_field" to include the ID of the question, so you can then include the ID in your "where" field of "answer_options". This will allow you to link the question.
I think that what you need is to set up the whole thing in a loop. query by custom field
Or you could store the ids of the questions you got above, an then, below, query for the answers for those specific posts.
Here's how I randomized my WordPress slider using the Advanced Custom Fields plugin + Royal Slider with a modified version of TheSwiftExchange's code above
<div id="full-width-slider" class="royalSlider heroSlider rsMinW">
<?php
/*
* Slider Repeater field shuffled
* http://stackoverflow.com/questions/12563116/incorporating-extra-loop-into-random-selection
*/
$rows = get_field('slider');
// For Debugging:
// echo "<pre>";
// var_dump($rows);
// echo "</pre>";
$quotes = get_field('slide_text'); // Get the number of images
shuffle($rows); // Randomize your slides
foreach ($rows as $row) {
$slideImageID = $row['slide_image'];
$slideImage = wp_get_attachment_image_src( $slideImageID, 'full' );
$slideText = $row['slide_text'];
?>
<div class="rsContent">
<div class="royalCaption">
<div class="royalCaptionInner">
<div class="infoBlock">
<?php if(!empty($slideText)) {
echo $slideText;
}; ?>
</div>
</div>
</div>
<img src="<?php echo $slideImage[0]; ?>" class="" />
</div><!-- /.rsContent -->
<?php } // end foreach ?>
</div><!-- /slider-wrap -->
Hi everyone I've been extracting rows from a SQL table 1 by 1. I wrote this code two years ago and realise how hideously ineffective it is. I'd like to speed things up by typing up a simple for-loop to automate the coding.
$maxRows_dd1 = 10;
$pageNum_dd1 = 0;
if (isset($_GET['pageNum_dd1'])) {
$pageNum_dd1 = $_GET['pageNum_dd1'];
}
$startRow_dd1 = $pageNum_dd1 * $maxRows_dd1;
$maxRows_dd2 = 10;
$pageNum_dd2 = 1;
if (isset($_GET['pageNum_dd2'])) {
$pageNum_dd2 = $_GET['pageNum_dd2'];
}
$startRow_dd2 = $pageNum_dd2 * $maxRows_dd2;
$maxRows_dd3 = 10;
$pageNum_dd3 = 2;
if (isset($_GET['pageNum_dd3'])) {
$pageNum_dd3 = $_GET['pageNum_dd3'];
}
$startRow_dd3 = $pageNum_dd3 * $maxRows_dd3;
... dd4 to dd99 go in between!
$maxRows_dd100 = 10;
$pageNum_dd100 = 99;
if (isset($_GET['pageNum_dd99'])) {
$pageNum_dd32 = $_GET['pageNum_dd99'];
}
$startRow_dd99 = $pageNum_dd99 * $maxRows_dd99;
which corresponds to:
mysql_select_db($database_rent, $rent);
$query_dd1 = "SELECT * FROM rent";
$query_limit_dd1 = sprintf("%s LIMIT %d, %d", $query_dd1, $startRow_dd1, $maxRows_dd1);
$dd1 = mysql_query($query_limit_dd1, $rent) or die(mysql_error());
$row_dd1 = mysql_fetch_assoc($dd1);
if (isset($_GET['totalRows_dd1'])) {
$totalRows_dd1 = $_GET['totalRows_dd1'];
} else {
$all_dd1 = mysql_query($query_dd1);
$totalRows_dd1 = mysql_num_rows($all_dd1);
}
$totalPages_dd1 = ceil($totalRows_dd1/$maxRows_dd1)-1;
mysql_select_db($database_rent, $rent);
$query_dd2 = "SELECT * FROM rent";
$query_limit_dd2 = sprintf("%s LIMIT %d, %d", $query_dd2, $startRow_dd2, $maxRows_dd2);
$dd2 = mysql_query($query_limit_dd2, $rent) or die(mysql_error());
$row_dd2 = mysql_fetch_assoc($dd2);
if (isset($_GET['totalRows_dd2'])) {
$totalRows_dd2 = $_GET['totalRows_dd2'];
} else {
$all_dd2 = mysql_query($query_dd2);
$totalRows_dd2 = mysql_num_rows($all_dd2);
}
$totalPages_dd2 = ceil($totalRows_dd2/$maxRows_dd2)-1;
mysql_select_db($database_rent, $rent);
$query_dd3 = "SELECT * FROM rent";
$query_limit_dd3 = sprintf("%s LIMIT %d, %d", $query_dd3, $startRow_dd3, $maxRows_dd3);
$dd3 = mysql_query($query_limit_dd3, $rent) or die(mysql_error());
$row_dd3 = mysql_fetch_assoc($dd3);
if (isset($_GET['totalRows_dd3'])) {
$totalRows_dd3 = $_GET['totalRows_dd3'];
} else {
$all_dd3 = mysql_query($query_dd3);
$totalRows_dd3 = mysql_num_rows($all_dd3);
}
$totalPages_dd3 = ceil($totalRows_dd3/$maxRows_dd3)-1;
... all the way to dd100!!!
How would I use a for loop to speed typing up all this code for each block of code from dd1 to dd100?
Read about arrays and for() loops.
This is a much more efficient code to do exactly what you do above:
<?php
// You only need to do these once as they are the same throughout
mysql_select_db($database_rent, $rent);
$maxRows = 10;
// This code gets the total number of rows in the database
$totalRowsAll = mysql_fetch_assoc(mysql_query("SELECT count(*) AS total FROM rent", $rent));
$totalRowsAll = (int) $totalRowsAll['total'];
?>
<div class="tab_container">
<div id="tab1" class="tab_content">
<table width="100%" border="0" cellspacing="5" cellpadding="5" id="1">
<?php
for ($i = 0; $i < 100; $i++) {
// Calcluate value for this iteration and query database
$pageNum = (isset($_GET['pageNum_dd'.($i + 1)])) ? (int) $_GET['pageNum_dd'.($i + 1)] : $i;
$startRow = $pageNum * $maxRows;
$query = "SELECT * FROM rent LIMIT $startRow, $maxRows";
$result = mysql_query($query, $rent) or die(mysql_error($rent));
$totalRows = (isset($_GET['totalRows_dd1'])) ? (int) $_GET['totalRows_dd1'] : $totalRowsAll;
${'totalPages_dd'.($i + 1)} = ceil($totalRows / $maxRows) - 1;
// Now print this row
?>
<tr height="100px" align="center">
<?php
while ($row = mysql_fetch_assoc($query)) {
?>
<td style="background-color: <?php echo $row['colour']; ?>;" onclick="window.location='pay.php?id=<?php echo $row['dNo']; ?>&user=<?php echo $username; ?>'" onmouseover="this.style.cursor='pointer'">
<form action="pay.php?id=<?php echo $row['dNo']; ?>&user=<?php echo $username; ?>" method="post">
<input type="hidden" id="<?php echo $row['dNo']; ?>">
<input type="hidden" value="<?php echo $username; ?>">
<button type="submit" class="link" id="t<?php echo $row['dNo']; ?>">
<span><?php echo $row['dNo']; ?></span>
</button>
</form>
</td>
<?php
} // End while
?>
</tr>
<?php
} // End for
?>
</table>
</div>
</div>
...however:
I'm fairly sure this could be summed up in a single query to get all the results you need, which would be much more efficient and drastically reduce database load. But because of the $_GET['pageNum_dd*'] and $_GET['totalPages_dd*'] variables which are used on a per row basis, I am not 100% sure about this, and I can't work out how this would be done without knowing more about what is produced. You need to examine whether or not these parameters that can be passed are actually necessary/useful. As it is, they may be cause rows of a varying length, with an unequal number of cells per row - which is probably not what you want.
The same also goes for the variables $totalPages_dd*, which are assigned below but never used anywhere. They may not be useful, and assigning them may be pointless.