Page 1 of 1

Mismatched array keys.

Posted: Fri Jan 09, 2004 5:02 pm
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:

Posted: Sat Jan 10, 2004 5:28 am
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.

Posted: Sat Jan 10, 2004 6:05 pm
by fungku
Thanks :)

Crystal Clear