Forum Moderators: coopster

Message Too Old, No Replies

div tags in php

arranging data based upon their position

         

Silent Miracle

8:58 am on Jul 13, 2011 (gmt 0)

10+ Year Member



hello! i need help regarding using div tag in php.. Actually what i want to do is to retrieve data from the database. In the table their is a position for each portion that is 1, 2, 3 position. I want to retrieve the data and display it on the basis of their position. I cant get it how to do that.. here is my code of what i have tried so far
I want to make a generic code that displays the content based upon their position retrieved from database.. Please help!
<?php
error_reporting(E_ALL);
mysql_connect("localhost","root","root");
$db=mysql_select_db("reportinfo");

$str="SELECT report_id FROM report WHERE report_id='1'";
$v=mysql_query($str) or die('Error'.mysql_error());
$s=mysql_fetch_array($v);

mysql_num_rows($v);
if ($v)
{
echo "query executed";
}


$st="SELECT * FROM section WHERE report_id ='".$s[0]."'";
$res=mysql_query($st) or die('Error'.mysql_error());
echo "<br/>";
echo mysql_num_rows($res);

if ($res)
{
echo" Query executed twice";
}

$kr=0;
while ($kr<mysql_num_rows($res))
{
$result=mysql_fetch_array($res);
if ($result['position']=='2')
{
echo "<div align=\"center\">";
echo $result['content'];
echo "</div>";
}
if ($result['position']=='3')
{
echo "<div align=\"center\">";
echo $result['content'];
echo "</div>";
}

$kr++;
}

?>

penders

9:31 am on Jul 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It looks like you just need to sort your SQL query...

$st="SELECT * FROM section WHERE report_id ='".$s[0]."' ORDER BY position";


Then just step through the results, they should now be in the correct order.

rocknbil

3:46 pm on Jul 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to retrieve the data and display it on the basis of their position.


Also if you want to order by specific positions, you can use an expression:

$st="SELECT * FROM section WHERE report_id ='".$s[0]."' ORDER BY position>3 desc, position<=3 asc";

... Will first order anything greater than 3 in descending order

8
6
5
4
followed by anything less than or equal to 3 in ascending order.

1
2
3

Aside, sorry, slightly off topic . . .

Don't do this.


while ($kr<mysql_num_rows($res))


There's no need to count the rows and use a separate incrementor to know where you are at (unless you need that number for something, which in most cases, you don't.) The while loop will iterate the rows until it's done. Do this instead.


$res=mysql_query($st) or die('Error'.mysql_error());
while ($result=mysql_fetch_array($res)) {
echo "
<div align=\"center\">" .
$result['content'] .
</div>
";
}


Also if you're only expecting a single result, you can use "if" instead of "while". The output result will be the same, but "while" implies there may be multiple records.

Silent Miracle

2:58 pm on Jul 14, 2011 (gmt 0)

10+ Year Member



Thanks penders and rocknbil for your help :)