Forum Moderators: coopster

Message Too Old, No Replies

auto increment question pls help

auto increment

         

kingkol

1:50 pm on Jan 26, 2011 (gmt 0)

10+ Year Member



hi
I am building "guest-book" project.Where guest post comments and those comments will be stored in database.In database table there is one column named "id" which is primary key and auto incremented.In display page(comment.php) where all comments will be displayed.---I want every comment to appear on alternative background color.like-dark-white-dark and so on.

I made this alternative background by---
<?php
$res=mysql_query("SELECT * FROM comment ORDER BY id ASC");
while($remark = mysql_fetch_array($res))
{
$c_id=$remark["id"];
if($c_id%2)
{
echo "<div class='grey'>";
}
else
{
echo "<div class='white'>";
}
?>


my problem is when I delete one comment from table then the alternative pattern of "dark-white-dark" does not work sequentially.I understand that happens because of auto-incrementation of "id"field.It does not update the id number automatically and leave the gap empty.like if i delete comment whose id is 5,then in database table its showing id-1,2,3,4,6. leaving id=5 empty or hidden.

I want id=6 to take place of id=5 or so on.is it possible? I am new to php and programming.

Please help
thanks in advance

omoutop

2:13 pm on Jan 26, 2011 (gmt 0)

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



stop using your table id and use an alternative number
small example:

function oddEven($num)
{
return ($num%2) ? TRUE : FALSE;
}

$rowCount = 0;
// execute your query here
while ($row3 = mysql_fetch_array($result3))
{
$rowCount++;

if (oddEven($rowCount) === TRUE){ echo '<div class='grey'>"';} else {echo "<div class='white'>";}
echo 'more data here';
echo '</div>'; // this is to close the grey/white div

}

kingkol

4:42 pm on Jan 26, 2011 (gmt 0)

10+ Year Member



Thank You Very much.You solve my problem.Thank You.

As i am quite new to php and programming as whole,I didn't understand the interline syntax of function oddeven($num).i.e.

return ($num%2) ? TRUE : FALSE;

I guess its shorthand of if-else.
and also in---if (oddEven($rowCount) === TRUE) there are 3equal operators are used.is it a typo? coz 2equals are also working fine.

Could you please suggest me where to look for the reference or how to proceed to learn php and mysql?

rocknbil

5:27 pm on Jan 26, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



// You may want to use these in other parts of your program
// define them anywhere, in a database field, config file, use constants if you like
$gray = 'grey';
$white = 'white';

$res=mysql_query("SELECT * FROM comment ORDER BY id ASC");
while($remark = mysql_fetch_array($res)){
$bg = ($bg==$gray)?$white:$gray;
echo "<div class=\$bg\">";
}


And yes, a ternary is shorthand for an if/else. If you want the white class first, switch them.

$bg = ($bg==$white)?$gray:$white;

omoutop

11:26 am on Jan 27, 2011 (gmt 0)

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




and also in---if (oddEven($rowCount) === TRUE) there are 3equal operators are used.is it a typo? coz 2equals are also working fine.


No typo.. 3 equal signs check for "complete match" instead of "mathc". Sory for my bad english, better read this to fully understand
http://gr.php.net/manual/en/language.operators.comparison.php