Search code examples
phpmysqldatabaserowrecord

can only get one value for $row['id']


I'm trying to get all the records in my database and it was successful, however I can only get 1 id value. I need to get all the id in my database and call a javascript function. In my code I can only get the id of the last record. How can I get them all?

<?php 
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("database_name");

$query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>" . $row['header'] . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
echo "<tr>";
$position=0; 
$message= $row['desc'];
$post = substr($message,$position,300); 
echo "<td>" . $post . "...</td>";
echo "</tr>";
echo "<tr>";
$id = $row['id'];
echo "<tr><td>";

/*The start of chaos */
echo "<form action='' method='post'  onSubmit='ReadMore()'>";
for($ctr=1;$ctr<=3;$ctr++){
$id=$ctr;  //This should change and increment the value of id, isn't it?

echo "<input type='hidden' name='more' value=".$id." id='more'/>";
}
echo "<input type='submit' name='read' value='Read More'>
</form>
</td></tr>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?> 

What's happening here is that it will display records from the database: $row['header'], $row['date'], $row['desc'] then a button which says Read More in a loop. What I would like to happen is to call a function after clicking 'read more', depending on the id.

For example,

___________________________
| This is the header      |
| Date                    |
| Description             |
| Read More Button        | //this button should display $id = 3
___________________________
| This is the header      |
| Date                    |
| Description             |
| Read More Button        | //this button should display $id = 2
___________________________
| This is the header      |
| Date                    |
| Description             |
| Read More Button        | //this button should display $id = 1
___________________________

Is that possible? or am I missing something. My code only displays $id=1 for all the buttons. I'm sorry for this.


Solution

  • you may try like this
    make the id field name as array like more[] and bring the form out side loop as shown 
    no need of for loop there to get the ids u already using while loop
    bring submit button also outside loop
    

    if u want to show the details of that post then why again u using form u can simply link it to the details page n can pass the id no need to submit it as a form .

    check the code below

    <?php 
    $con = mysql_connect('localhost','root','')
    or die(mysql_error());
    mysql_select_db ("database_name");
    
    $query = "SELECT * FROM news ORDER BY date DESC LIMIT 0 , 3";
    
    $result = mysql_query($query);
    
    //echo "<form action='' method='post'  onSubmit='ReadMore()'>";
    echo "<table>";
    
    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<th>" . $row['header'] . "</th>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>" . $row['date'] . "</td>";
    echo "</tr>";
    echo "<tr>";
    $position=0; 
    $message= $row['desc'];
    $post = substr($message,$position,300); 
    echo "<td>" . $post . "...</td>";
    echo "</tr>";
    echo "<tr>";
    $id = $row['id'];
    echo "<tr><td>";
    
    /*The start of chaos */
    
    //for($ctr=1;$ctr<=3;$ctr++){
    //$id=$ctr;  //This should change and increment the value of id, isn't it?
    
    //echo "<input type='hidden' name='more[]' value=".$id." id='more[]'/>";
    //}
     echo "<tr><td><a href='post_details.php?post_id=".$id."'>Read More</a></td></tr>";
    
    </td></tr>";
    echo "</tr>";
    }
    
    echo "</table>";
    //echo "</form>";
    mysql_close($con);
    ?>