Ok i am using codeIgniter to get resuts from database
class Video extends CI_Model{
public function getVideo()
{
$this->db->select('*');
$this->db->from('video');
// $this->db->where('category',$category);
return $this->db->get();
}
}
I am tring to display results from database in this way
<div>
first 10 results
</div>
<div>
second 10 results
</div
<div>
rest from query
</div>
Can some one help me with logic
foreach($results as r)
{
echo '<div>'.$r->video.'</div>';
}
Something like this?
$divisions = array(10,20);
for($i=0;$i<count($results);$i++){
if($i===0){
echo "<div>";
}else if(in_array($i,$divisions){
echo "</div><div>";
}else if($i===count($results)-1){
echo "</div>";
}
echo "<div>".$results[$i]->video."</div>";
}
Here you go. Tested and working.
<?php
//let's make a fake results array, just to show it works.
$results = array();
$c = 0;
while ($c < 100) {
array_push($results, array('video' => 'a'));
$c++;
}
//now for the actual answer.
$count = 0;
foreach ($results as $r) {
if ($count === 0) {
echo '<div>';
}
echo $r['video'] . ' ';
if ($count === 9 || $count === 19) {
echo '</div><div>';
}
$count++;
}
echo '</div>';
?>
Related
Good night all! Help please in the recursion, where I made a mistake, I need to output the categories, as in the last example!
Model:
protected function buildTree($data, $rootID = 0)
{
$tree = [];
foreach ($data as $id => $node) {
if ($node->parent_id == $rootID) {
unset($data[$id]);
$node->childs = $this->buildTree($data, $node->id);
$tree[] = $node;
}
}
return $tree;
}
public function getTree()
{
$data = Category::find()->all();
return $this->buildTree($data);
}
View:
<?php $form = ActiveForm::begin()
foreach ($tree as $cat) {
echo '<br><div class="spoiler-title">' . $cat['title'] . '</div>';
printNode($cat);
}
function printNode($cat, $level = 1)
{
if (count($cat['childs']) > 0) {
foreach ($cat['childs'] as $child) {
for ($j = 1; $j < $level; $j++) {
echo '------- <b>';
}
echo '' . $child['title'] . '</b><br>';
//echo $form->field($model, $child['title'])->checkbox();
//I want to do a checkbox, get an error, do not understand $
//form and $ model
if (count($child['childs']) > 0) {
printNode($child, $level + 1);
}
}
}
}
$form = ActiveForm::end() ?>
That's how I get the tree, this is with your code
root
category
category
category
---- <b>subcategory</b
---- <b>subcategory</b
---- <b>subcategory</b
---- <b>subcategory</b
category
category
I need to make checkboxes in subcategories, but I get an error, in the code I wrote in the comments, your code works, but I can not change it my needs as a picture.
You should also add recursion in view when you print tree.
Try this in view:
$form = \yii\bootstrap\ActiveForm::begin();
foreach ($tree as $cat) {
printNode($cat);
}
function printNode($cat, $level = 1) {
echo '<br><div class="spoiler-title">' . $cat['title'] . '</div>';
if (count($cat['childs']) > 0) {
foreach ($cat['childs'] as $child) {
echo \yii\helpers\Html::checkbox('someName[]') . ' ' . $child['title'] . '<br>';
if (count($child['childs']) > 0) {
printNode($child, $level + 1);
}
}
}
}
\yii\bootstrap\ActiveForm::end();
I am Trying to make a drop down menu, and list multiple items as well.
like for example:
here is my database:
if you see in that image you can see only one of the rows have a parent if i want multiple rows to have a parent of 'Far Far Away123' it only shows one of the items
<?php
function build_dropdown ($parent, $pages){
$items = "";
foreach($pages as $page){
// $items = "";
if($page['parent'] == $parent){
$items = $page;
} // END if
} // END foreach
if(is_array($items)){ // If a sub
echo '<ul id="sub_menu" class="sub_navagation'. $items['id'] .'">';
echo '<li>'.$items['page_name'].'</li>';
echo '</ul>';
} // END if
}// End Function
$sql = "SELECT * FROM pages ORDER by item_order";
$result = mysqli_query($db, $sql);
confirm_query($result);
while ($row = mysqli_fetch_assoc($result)) {
$pages[] = $row; // Add each row to $pages array to use later
}
foreach($pages as $key => $page){
if($page['parent'] == 'none'){ ?>
<li id = "<?php echo $page['id']; ?>">
<a href="page.php?id=<?php echo $page['id']; ?>" title="<?php echo $page['page_title']; ?>">
<?php echo $page['page_name']; ?>
</a>
<?php ?>
<?php
} // END if
build_dropdown($page['page_name'], $pages); // If there are child items then build them out
echo "</li> ";
} // END foreach
?>
Thanks
It is because you add only one.
You have a foreach searching all pages for subitems and remember only one of them for the latter if (is_array())
function build_dropdown($parent, $pages)
{
// item is an array
$items = array();
foreach($pages as $page) {
if ($page['parent'] == $parent) {
// add an element to the array
$items[] = $page;
} // END if
} // END foreach
if ($items) {
echo '<ul id="sub_menu" class="sub_navagation">';
foreach ($items as $item) {
echo '<li>'.$item['page_name'];
build_dropdown($item['page_name'], $pages);
echo '</li>';
} // END foreach
echo '</ul>';
} // END if
}// End Function
By the way, it would be better to start with the function giving build_dropdown($parent = 'none', $pages)
Try this inside your buildDropdown() function..
$items = array();
foreach($pages as $page)
{
if($page['parent'] == $parent)
{
$items[] = $page;
}
}
if (count($items) > 0)
{
echo '<ul id="sub_menu_'.$parent.'" class="sub_navagation">';
foreach($items as $item)
{
echo '<li>'.$item['page_name'].'</li>';
}
echo '</ul>';
}
My website currently ignores the first two images you place into the database and then proceeds to add images going across 5 columns and then moving down to the next row.
Update: Now it shows 3 of the 4 images in the database. Skips one image.
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
This is what my database looks like
http://i.stack.imgur.com/IFba8.jpg
This is what my website shows
http://i.stack.imgur.com/Wf7E1.jpg
Try this:
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
Please try this.
<?php
$i = 1;
echo "<table>";
while ( $row = $Recordset2->fetch_object() ) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
I am querying the database for data, but in order to do what I need, I end up looping through that results array at least three times. How do I get all of the info I need out of the array without having to loop so many times? I need to get data from the array based on the results from the previous loops. The code below is the only way I could figure out in order to only query the database once.
$recordSQL = mysqli_query($link, $sqlString);
$resultMonths = array();
while($recordResult = mysqli_fetch_assoc($recordSQL)) $resultMonths[] = $recordResult['date'];
mysqli_data_seek($recordSQL, 0);
$uniqueMonths = array_unique($resultMonths);
foreach($uniqueMonths as $key => $date){
echo '</div><div class="current-month">'.translateDate($date, '').'</div>';
$resultCompanies = array();
while($companyResult = mysqli_fetch_assoc($recordSQL)){
if($companyResult['date'] == $date) $resultCompanies[] = $companyResult['company'];
}
mysqli_data_seek($recordSQL, 0);
$uniqueCompanies = array_unique($resultCompanies);
$oldco = '';
foreach($uniqueCompanies as $key => $company){
$x = 0;
while($typeResult = mysqli_fetch_assoc($recordSQL)){
if($typeResult['date'] == $date && $typeResult['company'] == $company){
if($oldco != $typeResult['company']){
if($x != 0) echo '</div>';
echo '<div class="company-record">'.$typeResult['name'].' - ';
}
if($x > 0) echo ', ';
echo translateID('Type', $typeResult['type']).'('.translateID('Section', $typeResult['section']).')';
$oldco = $typeResult['company'];
$x++;
}
}
echo '</div>';
mysqli_data_seek($recordSQL, 0);
}
}
FYI, you are actually looping N**3 times. Do it this way:
$month_company_rows = array();
while ($row = mysqli_fetch_assoc($recordSQL)) {
$month_company_rows[$row['date']][$row['company']][] = $row;
}
foreach ($month_company_rows as $date => $company_rows) {
echo '</div><div class="current-month">'.translateDate($date, '').'</div>';
foreach ($company_rows as $company => $rows) {
echo '<div class="company-record">'.$company.' - ';
foreach ($rows as $x => $row) {
if($x > 0) echo ', ';
echo translateID('Type', $row['type']).'('.translateID('Section', $row['section']).')';
}
echo '</div>';
}
}
I want to mysql_fetch_array 100 items from mysql database. then every 25 items wrap a div.
also, I make a total items result check.
My code as below, but it will add <div> to every item, no as my require. So how to make it easier? Thanks.
if (mysql_num_rows($result) != 0) {
$total = mysql_num_rows($result);
$num = 1;
while ($row = mysql_fetch_array($result)) {
if ($num === 1) {
echo '<div>';
}
echo $row['title'] . '<br />';
if ($total < 26) {
echo '</div>';
}
else {
if ($num === 26) {
echo '<div>';
}
if ($total < 51) {
echo '</div>';
}
else {
if ($num === 51) {
echo '<div>';
}
if ($total < 76) {
echo '</div>';
}
else {
if ($num === 75) {
echo '<div>';
}
if ($total < 101) {
echo '</div>';
}
}
}
}
$num++;
}
}
You can use the mod operator.
See: http://php.net/manual/en/internals2.opcodes.mod.php
echo '<div>';
while($row = mysql_fetch_array($result)){
....
if (($num % 25) === 1) { echo '</div><div>' }
$num++;
}
echo '</div>';
Try This...
if(mysql_num_rows($result)!=0){
$total = mysql_num_rows($result);
$num=1;
while($row = mysql_fetch_array($result)){
if($num===1)
echo '<div>';
echo $row['title'].'<br />';
if($num==25){
echo '</div>';
$num=1;
}
$num++;
}
}
use the mod operator.
$num =1;
while($row = mysql_fetch_array($result)){
if (($num % 25) === 1) { echo '<div>' }
-------here data ----
if (($num % 25) === 0) { echo '</div>' }
$num++;
}
My advice is to separate your concerns... Give it a shot more like this:
// first get all your data.
$results = array();
while($row = mysql_fetch_array($result)){
$results[] = $row;
}
// now you have an array of all of your records.
if (!empty($results)) {
// total count of the array up front.
$total = count($results);
$interval = 0;
// save the data in a buffer for echoing later.
$buffer = array('<div>');
foreach($results as $row) {
$buffer[] = $row['title'] . '<br />';
if (++$interval === 24) { // notice the prefix ++
$interval = 0;
// close and re-open divs
$buffer[] = '</div><div>';
}
}
// one final close tag
$buffer[] = '</div>';
// now we zip it up and echo the content.
echo implode('', $buffer);
}
Here is how I do it...
$count = 0;
while($row = mysql_fetch_array($result)){
if ($count%$25 == 0)
{
echo "<div>";
}
//whatever your data goes here
count++
if ($count%$25 == 0)
{
echo "</div>";
}
}