5

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

4
  • be aware that this code is vulnerable to sql injection attacks. Commented Jun 27, 2011 at 11:53
  • Just as a sidenote... might fetch_assoc() ( php.net/manual/de/mysqli-result.fetch-assoc.php ) be better suited for your needs? Commented Jun 27, 2011 at 11:54
  • I might miss something, but you should put $i=0; before the foreach, and you don't need the [] after $motd there. Commented Jun 27, 2011 at 11:55
  • 3
    I should really have a cup of coffee before opening PhpStorm. Thanks everyone :) Commented Jun 27, 2011 at 11:59

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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:

  1. In foreach you have to specify the array itself, you do not need [].
  2. Always initialize your $i.
  3. You do not need &$value, you only need that reference if you want to modify your array in the foreach.

1 Comment

Thanks :) - notice that it should be $value['msg'] in line 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.