PHP Array is carrying too many elements - php

<?php
$query = "SELECT name FROM prodGroups";
$result = mysql_query($query);
$prodGroups = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$prodGroups[] = $row['name'];
}
if (count($prodGroups) == 0) // IF NO PRODUCT GROUPS EXIST
for ($j=1 ; $j<4 ; $j++)
{
echo "<li><a href='#'><span>Empty product group " . $j . "</span></a></li>";
}
else // FOR WHEN PRODUCT GROUPS DO EXIST
foreach ($prodGroups as $aGroup) // CYCLE THROUGH PRODUCT GROUPS
{
echo "<li class='submenu'><a href='#'><span>" . $aGroup . "</span></a>";
$query = "SELECT name FROM products WHERE prodGroup='$aGroup'";
$result = mysql_query($query);
for ($j=0 ; $j<count($prodGroups) ; ++$j)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) // PLACE PRODUCTS INTO AN ARRAY
**{
$products[] = $row['name'];
}**
if (!isset($products)) // IF THERE ARE NO PRODUCTS INSIDE A PRODUCT GROUP
echo "<ul><li><a href='#'><span>No products</span></a></li></ul></li>";
else // FOR WHEN PRODUCT(S) DO EXIST INSIDE A PRODUCT GROUP
{
echo "<ul>";
if(isset($products))
foreach ($products as $item) // CYCLE THROUGH PRODUCTS
{
echo "<li><a href='#'><span>" . $item . "</span></a>";
}
echo "</ul></li>";
}
}
}
?>
Where $products[] = $row['name']; it is carrying ALL the elements, whereas I just want it to carry product names to where they match the prodGroup name? I've tried using unset() to delete array contents in the loop, however this only allows one element in the array per cycle.

Add $products = array();
immediately after:
foreach ($prodGroups as $aGroup) // CYCLE THROUGH PRODUCT GROUPS
{

I tried to rewrite your loop, fixing several issues, most of them minor. I commented my changes. See if this works for you.
foreach ($prodGroups as $aGroup) // CYCLE THROUGH PRODUCT GROUPS
{
echo "<li class='submenu'><a href='#'><span>" . $aGroup . "</span></a>";
$query = "SELECT name FROM products WHERE prodGroup='$aGroup'";
$result = mysql_query($query);
//for loop here has been taken out
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) // PLACE PRODUCTS INTO AN ARRAY
{
$products[] = $row['name'];
}
if (!isset($products)) // IF THERE ARE NO PRODUCTS INSIDE A PRODUCT GROUP
echo "<ul><li><a href='#'><span>No products</span></a></li></ul>";
else // FOR WHEN PRODUCT(S) DO EXIST INSIDE A PRODUCT GROUP
{
echo "<ul>";
//if(isset($products)) - You already checked this..
foreach ($products as $item) // CYCLE THROUGH PRODUCTS
{
echo "<li><a href='#'><span>" . $item . "</span></a></li>"; //Don't forget to close this li
}
echo "</ul>";
}
echo '</li>'; //close li here
unset($products); //unset $products for next loop iteration
}
Of course, please do not use the deprecated mysql extension. See comment by #FreshPrinceOfSO.

This should be a little neater for you to see what I meant by using mysql_num_rows()
<?php
$query = "SELECT name FROM prodGroups";
$result = mysql_query($query);
if( (!$result) || (mysql_num_rows($result) == 0) )// IF NO PRODUCT GROUPS EXIST
{
for ($j=1 ; $j<4 ; $j++)
{
echo "<li><a href='#'><span>Empty product group " . $j . "</span></a></li>";
}
}
else // FOR WHEN PRODUCT GROUPS DO EXIST
{
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) // CYCLE THROUGH PRODUCT GROUPS
{
echo "<li class='submenu'><a href='#'><span>" . $row['name'] . "</span></a>";
$query2 = "SELECT name FROM products WHERE prodGroup='".mysql_real_escape_string($row['name'])."'";
$result2 = mysql_query($query2);
if( (!$result2) || (mysql_num_rows($result2)==0) )
{
echo "<ul><li><a href='#'><span>No products</span></a></li></ul>";
}
else
{
echo "<ul>";
while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) // PLACE PRODUCTS INTO AN ARRAY
{
echo "<li><a href='#'><span>" . $row2['name'] . "</span></a></li>";
}
echo "</ul>";
}
echo "</li>";
}
}
?>

Related

Add links to data in HTML table Created using PHP and a MYSQL database

I am trying to pull data from a table in PHPmyadmin and convert it to an HTML table based on some customer form input which filters out unneeded rows. The code below does that fine. The issue is that two of my columns need to contain links.
It would be easy enough to use PHP to change the table data into the link using a strtolower() and str_replace() to remove spaces, then concatinating the "www.website.com/" and the ".html". But I'm using a foreach loop to get all of the rows that I need and I don't know how to only alter one value per row.
I have tried using "Broswer Display Transformations" and "Input Transformations" in PHPmyadmin, but that only seems to affect the data in PHPmyadmin and not when I access the data via PHP.
My current code:
//* Code for Table
$query = "SELECT $searchFields FROM `hose_reels` $searchPhrase ORDER BY `model` ASC";
$result = mysqli_query($cxn,$query);
if ($row[$key] != "0") {
echo '<table width="100%" border="1" class="table"><tr>';
$row = $result->fetch_assoc();
foreach ($row AS $key => $value) {
$key = ucwords(str_replace('_', ' ', $key));
echo "<th>" . $key . "</th>";
}
echo "</tr>";
$result2 = mysqli_query($cxn,$query);
while($row = $result2->fetch_assoc()) {
echo "<tr>";
foreach ($row AS $key => $value) {
$row['$key'] = $value;
echo "<td>$row[$key]</td>";
}
echo "</tr>";
}
echo "</table>";
}
else {
echo "<p>No results match your selection. Please broaden your search.</p>";
}
Just add <a> tag in your php code. Below is the code. One more thing you have error in echo "<td>$row[$key]</td>"; line . it prints <td>$row[$key]</td> not the result you are fetching from DB.
echo '<table width="100%" border="1" class="table"><tr>';
$row = $result->fetch_assoc();
$i = 1;
foreach ($row AS $key => $value) {
$key = ucwords(str_replace('_', ' ', $key));
if($i == 1 || $i ==3){
echo "<th><a href='".key ."'" . $key . "</a></th>";
}else{
echo "<th>" . $key . "</th>";
}
$i++;
}
echo "</tr>";
$result2 = mysqli_query($cxn,$query);
$j =1;
while($row = $result2->fetch_assoc()) {
echo "<tr>";
foreach ($row AS $key => $value) {
$row['$key'] = $value;
if($i == 1 || $i ==3){
echo "<td><a href='".$row[$key]."'".$row[$key]."</a></td>";
}else{
echo "<td>$row[$key]</td>";
}
}
echo "</tr>";
}
echo "</table>";

Warp two row with a class after query

I want to warp every two row with a class. So I saw some guideline from here and googling and found foreach array_chunk to do this. And I tried as below which can't display any result. Without foreach its work well. Were is my wrong here please?
In the above picture that I want to do; my every two category warp with a class. And add a divider after each top category.
Here is my tried:
echo '<ul class="dropdown-menu dropdown-menu-large row">';
$sql = "SELECT id,name FROM main_cata ORDER BY id ASC";
$execute = $dbh->query("$sql");
$rowcount = $execute->num_rows ;
$row = $dbh->query($sql) ;
while ($row = $execute->fetch_assoc()) {
foreach (array_chunk($row, 2, true) as $array){
echo'<li class="col-sm-3">';
foreach($array as $rows){
echo '<ul><li class="dropdown-header">'.$rows->name.'</li>';
$sql2 = "SELECT id,name,page FROM catagory WHERE m_cata = '".$rows->id."' ORDER BY id ASC";
$execute2 = $dbh->query("$sql2");
$rowcount2 = $execute2->num_rows ;
$row = $dbh->query($sql) ;
while ($row = $execute2->fetch_assoc()) {
$cid = $row['id'];
$cname = $row['name'];
$page = $row['page'];
echo '<li>'.$cname.'</li>';
}
echo '<li class="divider"></li></ul>';
}
echo '</li>';
}
}
echo '</ul>';
while ($row = $execute->fetch_assoc()) {
fetch_assoc returns 1 row (id, name) from the query, then you are using the result and spliting in 2 (https://secure.php.net/manual/pt_BR/mysqli-result.fetch-assoc.php), What you can do is, make a counter instead of the array chunk that resets every time it reachs 2.
foreach (array_chunk($row, 2, true) as $array){
The content of $row is, for example, array('id' => 1, 'name' => 'Category 1').
foreach($array as $rows){
You are iterating again without needing to.
A simple version:
counter = 0;
echo "<ul>";
while (row = execute->fetch_assoc()) {
if (counter == 0) {
echo "init li";
}
echo "subinit li";
get subcategories
while (row2 = execute2->fetch_assoc()) {
print content
}
echo "subend li";
if (counter == 2) {
counter == 0;
echo "end li";
} else {
counter++;
echo "divider";
}
}
echo "</ul>";

Creating a menu consisting of multi-level structure, how to perform this more officiently? witout hard-coding limits?

I was asked to modify existing code to allow multi-level page structure. Each page can have one parent, and one page can have multiple children.
I will present following code. My question is is there a more efficient way of doing this? I am hard-coding the number of levels that are reachable.
Is there easier way to do this automatically without hard coding? Recursion perhaps?
function createMenu($parents) {
foreach ($parents as $parent) {
$parentName = $this->getPagefromID($parent);
$kids = $this->areThereKids($parent);
if ($kids == "yes") {
echo "<ul class=\"children\">\n";
$query = "SELECT * FROM pages";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$thisparent = $row['Parent'];
$name = $row['Name'];
$id2 = $row['ID'];
if ($thisparent == $parent) {
echo "<li>" . $name . "</li>\n";
if($this->areThereKids($id2) == "yes") {
$children = $this->getMyKids($id2);
foreach($children as $kid) {
$info = $this->getPersonInformation($kid);
$kidid = $info[0]["ID"];
echo "<li>" . $info[0]['Name'] . "</li>\n";
// more sub
if($this->areThereKids($kidid) == "yes") {
$children1 = $this->getMyKids($kidid);
foreach($children1 as $kid1) {
$info1 = $this->getPersonInformation($kid1);
$kidid1 = $info1[0]['ID'];
echo "<li>-" . $info1[0]['Name'] . "</li>\n";
if($this->areThereKids($kidid1) == "yes") {
$children2 = $this->getMyKids($kidid1);
foreach($children2 as $kid2) {
$info2 = $this->getPersonInformation($kid2);
$kidid2 = $info2[0]['ID'];
echo "<li>--" . $info2[0]['Name'] . "</li>\n";
// No need for 6 levels.
}
}
}
}
}
}
}
}
echo "</ul>\n";
}
echo "</li>\n";
}
}
This would be more efficiently done with recursion.
Identify the steps of the code that are repeated and turn them into a subroutine. From your code, we have the following repeated unit:
(...)
if($this->areThereKids($id2) == "yes") {
$children = $this->getMyKids($id2);
foreach($children as $kid) {
$info = $this->getPersonInformation($kid);
$kidid = $info[0]["ID"];
echo "<li>" . $info[0]['Name'] . "</li>\n";
// more sub
if($this->areThereKids($kidid) == "yes") {
(...)
Turn that into a function (assuming you're working with an object):
function dealWithKids($par) {
if ($this->areThereKids($par) == 'yes') {
$children = $this->getMyKids($par);
foreach ($children as $kid) {
$info = $this->getPersonInformation($kid);
$kidid = $info[0]["ID"];
echo "<li>" . $info[0]['Name'] . "</li>\n";
// instead of repeating the same steps again,
// call the dealWithKids function here
$this->dealWithKids($kidid);
}
}
}
...and then call that function instead of the repeated units of code:
while ($row = mysql_fetch_assoc($result)) {
$thisparent = $row['Parent'];
$name = $row['Name'];
$id2 = $row['ID'];
if ($thisparent == $parent) {
echo "<li>" . $name . "</li>\n";
// instead of this:
// if($this->areThereKids($id2) == "yes") {
// do this:
$this->dealWithKids($parent);
}
}

Unable to repeat/cycle through code with PHP and MySQL

<?php
$query = "SELECT name FROM prodGroups";
$result = mysql_query($query);
$prodGroups = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$prodGroups[] = $row['name'];
}
if (count($prodGroups) == 0 && (!isset($products))) // IF NO PRODUCT GROUPS EXIST
for ($j=1 ; $j<4 ; $j++)
{
echo "<li><a href='#'><span>Empty product group " . $j . "</span></a></li>";
}
else // FOR WHEN PRODUCT GROUPS DO EXIST
foreach ($prodGroups as $aGroup) // CYCLE THROUGH PRODUCT GROUPS
{
echo "<li class='submenu'><a href='#'><span>" . $aGroup . "</span></a>";
}
for ($k=0 ; $k<3 ; ++$k)
{
$query = "SELECT name FROM products WHERE prodGroup='$prodGroups[$k]'";
$result = mysql_query($query);
for ($j=0 ; $j<count($prodGroups) ; ++$j)
{
while ($row2 = mysql_fetch_array($result, MYSQL_ASSOC)) // PLACE PRODUCTS INTO AN ARRAY
{
$products[] = $row2['name'];
}
if (!isset($products)) // IF THERE ARE NO PRODUCTS INSIDE A PRODUCT GROUP
echo "<ul><li><a href='#'><span>No products</span></a></li></ul></li>";
else // FOR WHEN PRODUCT(S) DO EXIST INSIDE A PRODUCT GROUP
echo "<ul>";
if(isset($products))
foreach ($products as $item) // CYCLE THROUGH PRODUCTS
{
echo "<li><a href='#'><span>" . $item . "</span></a>";
}
echo "</ul>";
}
}
?>
For some reason, I can only get products to display on the last iterate of the loop, and not all of them. A variable is obviously being over written but I cannot identify which one nor can I over come it?
EDIT
Now I've incorporated appropriate indentation and curly braces, I'm experiencing issues where ALL products are now showing in only the last prodGroup rather than their relevant/appropriate ones
Use brackets in your loop, and for your own good, everywhere else.
for ($j=0 ; $j<count($prodGroups) ; ++$j)
$query = "SELECT name FROM products WHERE prodGroup='$prodGroups[$j]'";
$result = mysql_query($query); // <- this won't be included in your loop

Display php nested array results continuously in two column table

I have a nested array grouping three other arrays: $online, $busy and $offline, in order to display results in this exactly order.
$conteudo = array($online, $ocupado, $offline);
The desired result would be a table in two columns to display results in continuous flow like this:
online1 | online2
online3 | busy1
busy2 | offline1
offline2| offline3
offline4|
I've been trying lots of foreach loops and playing around changing the html tags, but the closest I can arrive of the desired result is this:
<table width='100%' cellpadding='5' border="1">
<?php
$i = 1; //contador de colunas
$max_colunas = 2; // numero de colunas
foreach ($conteudo as $row) {
echo "<tr>";
foreach ($row as $col) {
foreach ($col as $cell) {
if ($i == 1) {
echo "<td>" . $cell . "</td>";
} elseif ($i == $max_colunas){
echo "<td>" . $cell . "</td></tr>";
$i = 0;
} else {
echo "<td>" . $cell . "</td>";
}
$i++;
}
}
echo "</tr>";
}
This code will output a table like this:
onine1 | online2 |online3
busy1 | busy2 |
offline1|offline2 |offline3|offline4
I can't find out why it ignores completely $max_colunas, seems like it prints all the elements inside the array in row.
If I remove lines:
echo "<tr>";
echo "</tr>";
from beginning and end of foreach it will output all in a row like this:
onine1 | online2 |online3 | busy1 | busy2 |offline1|offline2 |offline3|offline4
Any suggestions to get the desired output format will be so appreciated.
Edited on 17/01:
This is how I'm getting the arrays from:
//group people
$online = array(); //group online
$ocupado = array(); //group ocupado
$offline = array(); //group offline
//select people to group
$atendentes = mysql_query("SELECT nome FROM atendentes ORDER BY nome") or die(mysql_error());
$atendentedb = array();
//put selected people in array
while ($row = mysql_fetch_assoc($atendentes)) {
$atendentedb[] = array('nome' => $row["nome"], 'online' => false);
}
//take people online now and check in selected people
$names = modWhosonlineCustom::getOnlineUserNames();
foreach ($names as $name):
//foreach ($atendentedb as $atendente):
for($i = 0; $i < count($atendentedb); $i++):
$att = strtolower($name->username);
if ($atendentedb[$i]['nome'] == $att):
$atendentedb[$i]['online'] = true;
break;
endif;
endfor;
endforeach;
//check each selected people
foreach ($atendentedb as $atendente) :
//save temporary data
$online_ = $atendente['online'];
$nome_ = $atendente['nome'];
//if selected people online
if ($online_) :
//take status to show
$status = mysql_query("SELECT status FROM atendentes WHERE nome = '$nome_' LIMIT 1") or die(mysql_error());
while ($row = mysql_fetch_assoc($status)):
$statusdb = $row["status"];
endwhile;
//verify and save deppending on status
switch ($statusdb):
//if online
case "disponivel":
$descricao = mysql_query("SELECT hp_online FROM atendentes WHERE nome = '$nome_' LIMIT 1") or die(mysql_error());
while ($row = mysql_fetch_assoc($descricao)):
$online[] = array('info'=>$row['hp_online']);
endwhile;
break;
//if busy
case "ocupado":
$descricao = mysql_query("SELECT hp_busy FROM atendentes WHERE nome = '$nome_' LIMIT 1") or die(mysql_error());
while ($row = mysql_fetch_assoc($descricao)):
$ocupado[] = array('info'=>$row['hp_busy']);
endwhile;
break;
endswitch;
//if offline
else:
$descricao = mysql_query("SELECT hp_offline, horario FROM atendentes WHERE nome = '$nome_' LIMIT 1") or die(mysql_error());
while ($row = mysql_fetch_assoc($descricao)):
$offline[] = array('info'=>$row['hp_offline'], 'horario'=>$row['horario']);
endwhile;
endif;
endforeach;
EDITED
So after following help instructions from DaveRandom I got to this code, which is really a drop away from the right format, except for the "mysterious" behaviour with results coming from array $offline, that are displaying all in "block" (all cells in a row, or all cells in a column) while the other arrays are displaying perfectly(??).
//group people
$online = $ocupado = $offline = array();
//select people to group
$query = "SELECT nome, status, hp_online, hp_busy, hp_offline, horario
FROM atendentes
ORDER BY nome";
$atendentes = mysql_query($query) or die(mysql_error());
$atendentedb = array();
// put selected people in array
while ($row = mysql_fetch_assoc($atendentes)) {
$atendentedb[strtolower($row['nome'])] = array_merge($row, array('online' => FALSE));
}
//take people online now and check in selected people
$names = modWhosonlineCustom::getOnlineUserNames();
foreach ($names as $name) {
$uname = strtolower($name->username);
if (isset($atendentedb[$uname])) $atendentedb[$uname]['online'] = TRUE;
}
//check each selected people
foreach ($atendentedb as $name => $atendente) {
//if selected people online
if ($atendente['online']) {
//verify and save deppending on status
switch ($atendente['status']) {
//if online
case 'disponivel':
$atendentedb[$name]['info'] = $online[] = $atendente['hp_online'];
break;
//if busy
case 'ocupado':
$atendentedb[$name]['info'] = $ocupado[] = $atendente['hp_busy'];
break;
}
//if offline
} else {
$atendentedb[$name]['info'] = $offline[] = $atendente['hp_offline'];
$atendentedb[$name]['info'] = $offline[] = $atendente['horario'];
}
}
//*******Display Results
$conteudo = array_merge($online, $ocupado, $offline);
$max_colunas = 2; // numero de colunas
// Start the table
echo '<table width="100%" cellpadding="5" border="1">'."\n";
// Loop all the objects
for ($i = 0, $j = 0; isset($conteudo[$i]); $i++) {
if ($j == 0) {
// Output the beginning of a row
echo " <tr>\n";
}
// Always output a data cell
echo " <td>$conteudo[$i]</td>\n";
if (++$j >= $max_colunas) {
// Output the end of a row and reset the cell counter
echo " </tr>\n";
$j = 0;
}
}
if ($j) {
// We may end up with an incomplete row at the end, so pad it with empty cells
// and close the row
while ($j++ < $max_colunas) {
echo " <td></td>\n";
}
echo " </tr>\n";
}
// Close the table
echo "</table>";
I would say that the first thing to do here is to "flatten" the array - having the multiple dimensions makes this a lot more complicated than it needs to be. So rather than creating $conteudo like this:
$conteudo = array($online, $ocupado, $offline);
...do this instead:
$conteudo = array_merge($online, $ocupado, $offline);
Then you can do this:
$max_colunas = 2; // numero de colunas
// Start the table
echo '<table width="100%" cellpadding="5" border="1">'."\n";
// Loop all the objects
for ($i = 0, $j = 0; isset($conteudo[$i]); $i++) {
if ($j == 0) {
// Output the beginning of a row
echo " <tr>\n";
}
// Always output a data cell
echo " <td>$conteudo[$i]</td>\n";
if (++$j >= $max_colunas) {
// Output the end of a row and reset the cell counter
echo " </tr>\n";
$j = 0;
}
}
if ($j) {
// We may end up with an incomplete row at the end, so pad it with empty cells
// and close the row
while ($j++ < $max_colunas) {
echo " <td></td>\n";
}
echo " </tr>\n";
}
// Close the table
echo "</table>";
See it working
EDIT
Try this code for generating your arrays. Note that the structure of your output arrays has been altered to fit my code sample above - if you use this data anywhere else in you script, you will need to modify that code as well. I have modified this so that there is only one database query, which would seem to be all that is required. I have also modified it so that the $atendentedb holds all the user data, including the status and info keys, and all rows contain a horario key.
Because of the fact that your input arrays originally contained more data than the ones created by this code will, the code may need further modification - but try this out and see how you get on.
//group people
$online = $ocupado = $offline = array();
//select people to group
$query = "SELECT nome, status, hp_online, hp_busy, hp_offline, horario
FROM atendentes
ORDER BY nome";
$atendentes = mysql_query($query) or die(mysql_error());
$atendentedb = array();
// put selected people in array
while ($row = mysql_fetch_assoc($atendentes)) {
$atendentedb[strtolower($row['nome'])] = array_merge($row, array('online' => FALSE));
}
//take people online now and check in selected people
$names = modWhosonlineCustom::getOnlineUserNames();
foreach ($names as $name) {
$uname = strtolower($name->username);
if (isset($atendentedb[$uname])) $atendentedb[$uname]['online'] = TRUE;
}
//check each selected people
foreach ($atendentedb as $name => $atendente) {
//if selected people online
if ($atendente['online']) {
//verify and save deppending on status
switch ($atendente['status']) {
//if online
case 'disponivel':
$atendentedb[$name]['info'] = $online[] = $atendente['hp_online'];
break;
//if busy
case 'ocupado':
$atendentedb[$name]['info'] = $ocupado[] = $atendente['hp_busy'];
break;
}
//if offline
} else {
$atendentedb[$name]['info'] = $offline[] = $atendente['hp_offline'].' '.$atendente['horario'];
}
}
I think you should update counter inside
foreach ($col as $cell) { } so it will count elements of the inner arrays and also move decalration of <tr> there.
if (!is_int($i/2)) { print '<tr><td>' }

Categories