query...">

Array problem

I am having problems 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 shows 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] => 
    )
)

Thus, 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: it will be easier for you to understand if you read this first: Optimize this SQL query

+5
source share
3 answers

First, your code will not work because of 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 using mysql_fetch_assoc instead of mysql_fetch_array

Thirdly, the initial value for $ i does not exist.

+3
source

1.) foreach ($motd[] as &$value) {  , foreach ($motd as &$value) {

2.) for() foreach.

    for($a=0, $cnt=count($motd)-1; $a<=$cnt; $a++ )
    { 
        if($motd[$a]["msg"] != "" )
        { 
          #do something here 
        }
    }
+1

I rewrote your code a bit. There is no need to define all HTML several times just because it has a small change (I noticed only 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:

  • In foreachyou need to specify an array, you do not need [].
  • Always initialize yours $i.
  • You do not need &$value, you need this link only if you want to change your array to foreach.
+1
source

All Articles