0

I'm currently working on my computing project which would allow a user to create an indefinite amount of files. What I'm currently stuck on is retrieving these values to let a user select one here is my code:

 function DispMonth(){
 $dateList=GetDates($_Session['user']);//use session id
foreach($dateList as $value){//get each value
    echo '<tr>' .$value.'</tr>';}
}
?>
<body>
<table>
    <th><tr> Made Budgets</tr> </th>
    <?php DispMonth(); ?>
</body>
</html>

My GetDates function is:

function GetDates($id){
$result=mysql_query('select monthYear from database where userId='.$_Session['user'].'');
while ($row=mysql_fetch_object($result)){
    $x[]=$row['monthYear'];}
return x;
}

Essentially I would like the table to look something like this:

|monthYear| edit | delete |

With edit and delete being links/buttons which would send the value of monthYear to a new php page to fetch all the values.(Monthyear is a primary key in my sql table)

Awtthem
  • 1
  • 4

1 Answers1

0

There are few issues with your code which you need to fix, and few changes you need to make to make it work, such as:

  • Your table structure is wrong. <tr> cannot be inside <th>, also the closing </table> tag is missing.

  • You're fetching rows from the result set using mysql_fetch_object() function but accessing the column values using $row[...], which is wrong. Use mysql_fetch_array() instead.

    while ($row=mysql_fetch_array($result)){ ...
    
  • Given the fact that monthYear is the primary key of your table, make use of this column value in edit and delele buttons in your foreach loop, like this:

    <a href="newPage.php?monthYear=<?php echo $value ?>&edit">edit</a>
    <a href="newPage.php?monthYear=<?php echo $value ?>&delete">delete</a>
    

    Later, on newPage.php page, you can edit or delete any particular row like this:

    if(isset($_GET['edit'])){
        $monthYear = $_GET['monthYear'];
        // Edit the row
    }
    
    if(isset($_GET['delete'])){
        $monthYear = $_GET['monthYear'];
        // Delete the row
    }
    

So your functions and table structure would be like this:

<table>
    <tr>
        <th>Made Budgets</th>
        <th>Edit</th>
        <th>Delete</th>
    </tr>
    <?php DispMonth(); ?>
</table>

function DispMonth(){
    $dateList=GetDates($_Session['user']);
    foreach($dateList as $value){
        ?>
        <tr>
            <td><?php echo $value; ?></td>
            <td><a href="newPage.php?monthYear=<?php echo $value ?>&edit">edit</a></td>
            <td><a href="newPage.php?monthYear=<?php echo $value ?>&delete">delete</a></td>
        </tr>
        <?php
    }
}

function GetDates($id){
    $result=mysql_query('select monthYear from database where userId='.$_SESSION['user'].'');
    $x = array();
    while ($row=mysql_fetch_array($result)){
        $x[] = $row['monthYear'];
    }
    return x;
}

Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37