I am running into issues with the following code:
$ids = '"' . implode('", "', $crumbs) . '"';
$motd = array();
$dober = $db->query("SELECT id, name, msg, datetime FROM tbl_depts td INNER JOIN tbl_motd tm ON td.id = tm.deptid WHERE td.id IN (" . $ids . ")");
while ($row = $dober->fetch_array()) {
$motd[] = $row;
}
A print_r reveals this:
Array
(
[0] => Array
(
[0] => 1
[id] => 1
[1] => Management
[name] => Management
[2] => New Management Rule!
[msg] => New Management Rule!
[3] =>
[datetime] =>
)
[1] => Array
(
[0] => 2
[id] => 2
[1] => Human Resources
[name] => Human Resources
[2] => DPS
[msg] => DPS
[3] =>
[datetime] =>
)
)
As such, I cannot use this code to generate things:
foreach ($motd[] as &$value) {
if ($motd['msg'] != "") {
if ($i == 0) {
?>
<li><a href="#" title="content_<?php echo $value['id']; ?>"
class="tab active"><?php echo $value['name']; ?></a></li>
<?
} elseif ($i == $len - 1) {
?>
<li><a href="#" title="content_<?php echo $value['id']; ?>"
class="tab"><?php echo $value['name']; ?></a></li>
<?php } else { ?>
<li><a href="#" title="content_<?php echo $value['id']; ?>"
class="tab"><?php echo $value['name']; ?></a></li>
<?
}
$i++;
}
}
Any ideas on what I'm doing wrong here?
EDIT: you might find it easier to understand if you read this first: Optimize this SQL query
Issues with Array There are significant weaknesses when working with arrays, these includes: Fixed-size (We can create a structure with a fixed number of member variables, but we can't change that amount) Expensive to move many of the entries in an array. All the entries of an array must be of the same type.
The biggest limitation of array is that we need to define the size of array beforehand. This is possible only if the maximum size of array is known beforehand and may lead to memory wastage.
First - your code will not work because of this two lines:
foreach ($motd[] as &$value) {
if ($motd['msg'] != "") {
You should use $motd, not $motd[] in foreach and check $value['msg'], not $motd['msg']
Second, try to use mysql_fetch_assoc instead of mysql_fetch_array
Third - there is no initial value for $i.
1.) You might have an issue with foreach ($motd[] as &$value) {
perhaps it should be foreach ($motd as &$value) {
2.) I would rather use a for() loop instead of a foreach.
for($a=0, $cnt=count($motd)-1; $a<=$cnt; $a++ )
{
if($motd[$a]["msg"] != "" )
{
#do something here
}
}
I have rewritten your code a bit. No need to define the whole HTML several times only because there is a small change in it (I only spotted active
).
$i=0;
foreach ($motd as $value) {
if ($value['msg'] != "") {
$active = $i == 0 ? ' active' : ''; //based on the value of `$i`
?>
<li>
<a href="#"
title="content_<?php echo $value['id']; ?>"
class="tab<?php echo $active?>"><?php echo $value['name']; ?></a></li>
<?php
$i++;
}
}
As I noted in the comments earlier:
foreach
you have to specify the array itself, you do not need []
.$i
.&$value
, you only need that reference if you want to modify your array in the foreach
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With