Mismatched array keys.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fungku
Forum Newbie
Posts: 5
Joined: Wed Dec 31, 2003 11:59 pm

Mismatched array keys.

Post by fungku »

I have a problem.
As seen here.
(edit: I think I temporarily fixed the problem by renaming the ids directly in the mysql table, they were previously 7,10,9 in that order and are now 1,2,3)

There are 2 reviews that link to eachother instead of themselves.

here is the code I use to display them, can anyone figure out what's wrong here:

Code: Select all

$_info = array();
 $_info = get_titles_ids();
 $title = preg_grep('/^' . $letter . '/i', $_info['titles']);

foreach ($title as $list_item)
 {
    $id_key = array_search($list_item, $_info['titles']);
    $id = $_info['ids'][$id_key];
    echo '<a href="?page=review&action=review&rid=' . $id . '">' . $list_item . '</a>';
 }
and relative part from the function get_titles_ids() if you so wish

Code: Select all

$query = "SELECT title FROM alc_reviews";
		$result = mysql_query($query) or die ('I cannot receive from the database because: ' . mysql_error());
		while($row = mysql_fetch_row($result))
		{
			$_info['titles'][] = $row[0];
		}
		
		$query = "SELECT id FROM alc_reviews";
		$result = mysql_query($query) or die ('I cannot receive from the database because: ' . mysql_error());
		while($row = mysql_fetch_row($result))
		{
			$_info['ids'][] = $row[0];
		}
Anyone? :cry:
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Consider the following:

Code: Select all

$query = "SELECT id, title FROM alc_reviews";
      $result = mysql_query($query) or die ('I cannot receive from the database because: ' . mysql_error());
      while($row = mysql_fetch_row($result))
      {
         $_info['titles'][$row[0]] = $row[1];
      }
By doing this, you get an array holding both the keys and the titles. You likely need to rewrite that part abit to fit you, but the general idea is there.
That way, you can skip the second query, and you still can use dynamic id's (be able to delete a row without rearranging the id's in the table manually).

Code: Select all

// explanation; your idea of foreach loop change
$id[1] = "foo";
$id[2] = "bar";
$id[3] = "foofoo";
$id[5] = "barbar";
$id[6] = "foobar";
foreach ($id as $k => $val) {
    echo '<a href="index.php?id='.$k.'">'.$val.'</a><br />';
}
Hope I was clear enough.
fungku
Forum Newbie
Posts: 5
Joined: Wed Dec 31, 2003 11:59 pm

Post by fungku »

Thanks :)

Crystal Clear
Post Reply