Forum Moderators: coopster

Message Too Old, No Replies

mysql_fetch_array() question

         

doozer77

12:16 pm on Jun 27, 2005 (gmt 0)

10+ Year Member



Here's my problem:

I want to retreive six rows from a table and have the elements of each row displayed in a different place in the template page. I could just run the results of mysql_fetch_array() through a while loop and build a table but I'm trying to implement this into an existing design and to rebuild it would be very messy.

Now if I were dealing with only one $row from the database query I could access each element through $row[index_number] and place it whereever I want. But beacuse I'm returning six results I don't kow how to specify which $row I want to manipulate.

Perhaps this is a simple solution and I've confused myself. Any ideas?

doozer

roldar

12:37 pm on Jun 27, 2005 (gmt 0)

10+ Year Member



Though I'm not sure of the syntax, I think the answer you're looking for might be a 2-dimensional array.

Again, I'm not sure exactly how to do it, but I would imagine it goes something like this:

for($z=0;$z<mysql_num_rows($data);$z++)
{
$dbase_rows[$z]=mysql_fetch_array($data, MYSQL_ASSOC);
}

Then when you want to output it, you would do something like:

echo $dbase_rows[1]['city'];
echo $dbase_rows[1]['state'];
echo $dbase_rows[1]['zip_code'];

The second row's data could be accessed with:

echo $dbase_rows[2]['city'];
echo $dbase_rows[2]['state'];
echo $dbase_rows[2]['zip_code'];

etc.

The PHP gods will hopefully give you more help if I'm offbase here.

mcibor

2:13 pm on Jun 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can do it as roldar suggested. I would suggest however a bit other option: Add LIMIT 0, 6 to your sql query to limit the output to six rows, and you may prefer to use other structure to create the 2D table - one that is a bit more general. The code will look like this:

<?php
$sql = "SELECT * FROM table LIMIT 0, 6";
//connect to db... You know how to connect:)
//ask the query
$query = mysql_query($sql) or die(mysql_error());
$table = array();//define a table to store info

while($row = mysql_fetch_assoc($query))
{
$table[] = $row;
}
?>

You've got yourself the same array as roldar did. However here if you somewhen would want to change the output to display 10, then you just change the sql.

best regards
Michal Cibor

PS. I'm no php god <lol> and I don't know no one that is

doozer77

7:02 pm on Jun 27, 2005 (gmt 0)

10+ Year Member



Thanks roldar & mcibor. Your ideas worked perfectly.

One new problem though. I have an admin page where the user can call forth database entries to edit them. There is a textarea with multiple lines of text and the problem is that everytime I submit the form, thus updating the db, a new <br /> tag is added to each line of text in the textarea field. Which means the more the user edits that particular post the more <br /> tags are written in and thus more space between lines in the final presentation. How can I fix this?

doozer

mcibor

9:17 pm on Jun 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I presume you are using nl2br function. Try to get rid of sparse \n (new line)

$textarea = "Some text
more text

and more text - notice the double new line above";

$text_corrected = str_replace("\n\n", "\n", $textarea);

/*It's now: "Some text
more text
and more text - ...."; no double lines

Best regards
Michal

PS This is not optimal method, because if there are 3 new lines they are converted to two, not one. However it should work in your case.