Php How to auto increment character? - php

<?php
try {
$stmt2 = $db->query("SELECT cid, parent, name FROM category");
$row_count = $stmt2->rowCount();
if ($row_count) {
$rows = $stmt2->fetchAll(PDO::FETCH_ASSOC);
}
} catch (PDOException $e) {
echo $e->getMessage();
}
$items = $rows;
$id = '';
echo "<select>";
foreach ($items as $item) {
if ($item['parent'] == 0) {
echo "<option><a href='#'>".$item['name']."</a>";
$id = $item['cid'];
sub($items, $id);
echo "</option>";
}
}
echo "</select>";
function sub($items, $id){
foreach ($items as $item) {
if ($item['parent'] == $id) {
$x = '-';
$x++;
echo "<option>".$x."<a href='#'>".$item['name']."</a>";
sub($items, $item['cid']);
echo "</option>";
}
}
}
?>
This is my dropdown menu code. I want this "-" character in each parent item and auto increment this character in each parent item.
Like this select menu:

I think you need to use str_repeat()
example:
foreach ($items as $item) {
if ($item['parent'] == 0) {
echo "<option><a href='#'>".$item['name']."</a>";
$id = $item['cid'];
sub($items, $id, 1);
echo "</option>";
}
}
echo "</select>";
function sub($items, $id, $counter){
foreach ($items as $item) {
if ($item['parent'] == $id) {
echo "<option>".str_repeat("-",$counter)."<a href='#'>".$item['name']."</a>";
sub($items, $item['cid'],$counter+1);
echo "</option>";
}
}
}

Related

Yii2 recursion of the tree menu?

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();

PHP - Drop down not showing all items in mysql

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>';
}

Exclude specific value(s) from php array

If I want to exclude 2 specific field ids from this code, what needs to change??
// foreach field, print the option
echo '<select name="sort" id="pref_sortby">';
if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'name')) {
echo '<optgroup label="'.get_string('fields', 'data').'">';
foreach ($fields as $field) {
if ($field->id == $sort) {
echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
} else {
echo '<option value="'.$field->id.'">'.$field->name.'</option>';
}
}
echo '</optgroup>';
}
// foreach field, print the option
echo '<select name="sort" id="pref_sortby">';
if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'name')) {
echo '<optgroup label="'.get_string('fields', 'data').'">';
foreach ($fields as $field) {
// add this condition, replacing 50 and 60 with the IDs you want to exclude
if (in_array($field->id, array(50, 60))) {
continue;
}
if ($field->id == $sort) {
echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';
} else {
echo '<option value="'.$field->id.'">'.$field->name.'</option>';
}
}
echo '</optgroup>';
}

Echo all row's into table

So i have a database with 33 column's
Now this is my query :
$q3 = "SELECT * FROM qbd";
$r3 = $db1->query($q3);
while ($result = $r3->fetchAll()){
foreach($result as $row3){
echo "<tr>";
echo "<td colspan='2'>Kill ".$row3['ID']."</td>";
echo "<td>".$row3['Dragon_Bones']."</td>";
echo "<td>".$row3['Royal_Dragonhide']."</td>";
echo "</tr>";
}
}
How can i make it so that i only need to have 1 $row3 and not need to write all the column names in it?
You can try fetch() and fetch associate instead of fetchAll. Try this.
while ($result = $r3->fetch(PDO::FETCH_ASSOC)){
echo '<tr>';
foreach($result as $value){
echo '<td>'.$value.'</td>';
}
echo '</tr>';
}
You can use a second foreach to traverse to array you get as follows:
while ($result = $r3->fetchAll())
{
foreach($result as $row3)
{
echo "<tr>";
foreach($row3 as $key=>$val)
{
echo $key;
echo $val;
}
echo "</tr>";
}
}
This will display the key (column name) and the value in it.
If you want to just display the values, you can use this instead:
while ($result = $r3->fetchAll())
{
foreach($result as $row3)
{
echo "<tr>";
foreach($row3 as $key=>$val)
{
echo $val;
}
echo "</tr>";
}
}
If you are just trying to put out a table of the rows, and want the headings on the first row then you can use mysqli_data_seek() to reset the pointer back to the start after processing the first row to get the titles.
Something like this (assuming you can use mysqli_fetch_assoc):-
<?php
$q3 = "SELECT * FROM qbd";
$r3 = $db1->query($q3);
if ($result = $r3->fetch_assoc())
{
echo "<tr>";
foreach($result as $row3_key=>$row3_value)
{
echo "<td>".$row3_key."</td>";
}
echo "</tr>";
$db1->data_seek(0);
while ($result = $r3->fetch_assoc())
{
echo "<tr>";
foreach($result as $row3_key=>$row3_value)
{
echo "<td>".$row3_value."</td>";
}
echo "</tr>";
}
}
?>
EDIT - if your db class doesn't support mysqli_data_seek() (or if you are using an unbuffered query):-
<?php
$q3 = "SELECT * FROM qbd";
$r3 = $db1->query($q3);
if ($result = $r3->fetch_assoc())
{
echo "<tr>";
foreach($result as $row3_key=>$row3_value)
{
echo "<td>".$row3_key."</td>";
}
echo "</tr>";
OutputRow($result);
while ($result = $r3->fetch_assoc())
{
OutputRow($result);
}
}
function OutputRow($result)
{
echo "<tr>";
foreach($result as $row3_key=>$row3_value)
{
echo "<td>".$row3_value."</td>";
}
echo "</tr>";
}
?>
$keys = array();
$values = array();
echo '<table>';
while ($result = $r3->fetchAll()){
foreach($result as $row3){
foreach($row3 as $key=>$val){
for($i=1;$i=30;$i++){
$keys[]=$key;
}
$values[] = $val;
}
echo '<tr>';
foreach($keys as $k){
echo $k;
}
foreach($values as $v){
echo $v;
}
echo '</tr>';
}
}
echo '</table>'

php/mysql array or query with some values

Hello I have this query
$upit = "SELECT id from usluga";
$rezultat = mysql_query($upit);
while($red = mysql_fetch_object($rezultat))
{
echo "<li>{$red->id}</li>";
}
I want on exit to get this:
<li>1,2,3,4,5,6</li>
<li>7,8,9,10,11,12</li>
<li>13,14,15,16,17,18</li>
<li>19,20,21,22,23,24</li>
and so on..
Any help?
You can use the mod operator(%) or use array functions to achive this
$upit = "SELECT id from usluga";
$rezultat = mysql_query($upit);
while ($red=mysql_fetch_object ($rezultat)) {
$t[] = $red->id;
}
$chunks = array_chunk($t, 6);
foreach ($chunks as $chunk) {
echo "<li>" . join(",", $chunk) . "</li>";
}
try this..
$upit="SELECT id from usluga";
$rezultat=mysql_query($upit);
$i=0;
$liflag=0;
while ($red=mysql_fetch_assoc($rezultat))
{
if($i<6)
{
$liflag=1;
if($i==0)
{
echo "<li>$red->id";
$i++;
continue;
}
else
{
echo ",$red->id";
$i++;
continue;
}
}
else
{
echo ",$red->id</li>";
$i=0;
$liflag=0;
continue;
}
}
if($liflag==1)
{
echo "</li>";
}

Categories