Forum Moderators: coopster
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
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.
<?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 infowhile($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
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
$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.