Renumber MySQL UID field

From: Peter (BOUGHTONP)18 Jun 2012 15:15
To: koswix 16 of 29
Learn some PHP - you would have also said you're unlikely to ever need it before finding out about this, right?

If "all the stuff online" is telling you how to deal with single rows, then you're not looking at all the stuff (or are misunderstanding it, thus learning a bit would be beneficial).

In any case, Stack Overflow has this question asked/answered several times. Like here.
From: koswix18 Jun 2012 16:02
To: Peter (BOUGHTONP) 17 of 29
But why learn when I can get you to find the answer for me?! (kiss)
From: koswix18 Jun 2012 23:00
To: Peter (BOUGHTONP) 18 of 29
Your solution doesn't work :(
From: Drew (X3N0PH0N)18 Jun 2012 23:05
To: koswix 19 of 29
You must be doing it wrong.

code:
$query = mysql_query("SELECT text FROM `content` ORDER BY RAND() LIMIT 0,6");
$hooray = array();
while($result = mysql_fetch_array($query)) {
    $hooray[] = $result;
}
 
print_r($hooray);


(last line just prints the array)
From: koswix18 Jun 2012 23:16
To: Drew (X3N0PH0N) 20 of 29

Ah-hah. The problem is with how I'm trying to print the array elements.

 

If I use "print" it just outputs "Array". print_r prints the content, but does it like this:

 

Array ( [0] => The service was excellent. [text] => The service was excellent. )

 

How on earth do i just get the bit of text I need out of it?! This shit was so easy in QBASIC :(

From: Drew (X3N0PH0N)18 Jun 2012 23:18
To: koswix 21 of 29
(just a note: that will result in an array of arrays. i.e. $hooray will be an array containing arrays containing the result elements. So the first element of the first result will be at $hooray[0][0], second element of the first result at $hooray[0][1] and first element of the second result at $hooray[1][0] and so on)
From: Peter (BOUGHTONP)18 Jun 2012 23:19
To: koswix 22 of 29
It's not my solution, but it works perfectly.
From: Drew (X3N0PH0N)18 Jun 2012 23:19
To: koswix 23 of 29
As above. I think fetch_array produces both an indexed and an associative array (or one which acts as both, I dunno (and they might not be the right words)).

So you can do either:

$hooray[0]['text'] or $hooray[0][0]

They will point to the same thing.
From: koswix18 Jun 2012 23:21
To: Peter (BOUGHTONP) 24 of 29
Tis, cos you made me look at it. But you failed to adjust it to fit my exact requirements.
From: koswix18 Jun 2012 23:24
To: Drew (X3N0PH0N) 25 of 29

(hug)

 

Thank you! That was the vital bit of information that I couldn't be bothered getting my huge, and severely out of date, PHP book down off the shelf for (kiss)

From: Drew (X3N0PH0N)18 Jun 2012 23:25
To: koswix 26 of 29
If you're simply taking 6 results with one field each then it's just $array[0][0] to $array[5][0]
EDITED: 18 Jun 2012 23:26 by X3N0PH0N
From: Peter (BOUGHTONP)18 Jun 2012 23:29
To: koswix 27 of 29
You didn't hire me to fit it to your exact requirements.
From: koswix19 Jun 2012 00:08
To: Drew (X3N0PH0N) 28 of 29

Yay, it's all working now, I think I can forget about it now.

 


Pete: It was implied.

From: Drew (X3N0PH0N)19 Jun 2012 00:35
To: koswix 29 of 29
(cheer)