Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with array

Tags:

arrays

php

mysql

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

like image 255
bear Avatar asked Jun 27 '11 11:06

bear


People also ask

What are problems with array?

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.

Which is the biggest problem of array Mcq?

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.


3 Answers

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.

like image 141
Sergii Nester Avatar answered Oct 21 '22 04:10

Sergii Nester


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 
        }
    }
like image 34
Michael Brevig Avatar answered Oct 21 '22 06:10

Michael Brevig


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.
like image 1
kapa Avatar answered Oct 21 '22 04:10

kapa