Gowri Sankar
Gowri Sankar

Reputation: 99

How to get one value from table in php when table written in while loop?

I wrote a query to select list from job table and display it in table but now I want to get only one value from the table that is id to activate or deactivate the job I am struck here my code is

<table class="table table-bordered table-hover table-striped">
<thead>
    <tr>
        <th>JOb ID</th>
        <th>CompanyID</th>
        <th>Job Description</th>
        <th>Duration</th>
        <th>Budget</th>
        <th>KeySkills</th>
        <th>Joining Date</th>
        <th>Expiry Date</th>
        <th>Min Experience</th>
        <th>Max Experience</th>
        <th>Status</th>
        <th>Set Status</th>        
    </tr>
</thead>
<tbody>
<?php

$res=mysql_query("SELECT * FROM job ORDER BY jid DESC");

while($row=mysql_fetch_array($res))
{
$num_rows = mysql_num_rows($res);

 ?>
<tr>
<td><p><?php echo $row['jid']; ?></p></td>
<td><p><?php echo $row['cid']; ?></p></td>
<td><p><?php echo $row['jdesc']; ?></p></td>
<td><p><?php echo $row['duration']; ?></p></td>
<td><p><?php echo $row['budget']; ?></p></td>
<td><p><?php echo $row['keyskills']; ?></p></td>
<td><p><?php echo $row['jdate']; ?></p></td>
<td><p><?php echo $row['edate']; ?></p></td>
<td><p><?php echo $row['cdexmin']; ?></p></td>
<td><p><?php echo $row['cdexmax']; ?></p></td>
<td><p>
<?php if($row['status']=="active")
{ 
echo "Active";
}
else {
echo "Inactive";
} ?></p></td>
<td>
  <form  action="" method="post"> <input type="submit"  name="submit"      value="activate"  />
<?php
if($_POST['submit']=='activate')
{

   $jid=$_POST['jid'];
    $delet_query = mysql_query("UPDATE job SET status='active' WHERE jid='". $jid."' ") or die(mysql_error());

 if($delet_query) {
  echo 'job with id '.$row[jid].' is activated, to refresh your page, click'.  '<a href='.$_SERVER["PHP_SELF"].' > here </a>';

   }
   }?>
</form>
<form action="" method="post"><?php $_POST['jid']=$row['jid'] ?><input type="submit"   name="submit"   value="deactivate"  />

<?php 
if($_POST['submit']=='deactivate')
{
     $jid=$_POST['jid'];
     $delet_query = mysql_query("UPDATE job SET status='deactive' WHERE jid='".     $jid."'  ") or die(mysql_error());

   if($delet_query) {
    echo 'job with id '.$row[jid].' is deactivated, to refresh your page, click'.       '<a href='.$_SERVER["PHP_SELF"].' > here </a>';

     }
    }?>
</form>
</td>        
</tr>
<?php
}
?>

<tr> 
<td> <p>Total Jobs:<?php echo $num_rows; ?></p></td>
</tr>

                                    </tbody>
                                </table>

I need to activate only one value from the table with id so please help me in a way that I created database table

Upvotes: 0

Views: 83

Answers (3)

Sivaguru
Sivaguru

Reputation: 57

Use the below code it will work

  <?php 
  if(isset($_GET['jid'])) 
  {
  $jid=$_GET['jid'];
  $status=$_GET['status'];
  if($status=='active') { $new_status='inactive'; } else { $new_status='active'; }
  $delet_query = mysql_query("UPDATE job SET status='$new_status' WHERE jid='". $jid."' ") or die(mysql_error());
  if($delet_query) {
   echo 'job with id '.$row[jid].' is activated, to refresh your page, click'.  '<a href='.$_SERVER["PHP_SELF"].' > here </a>';
   }
   }
    <table class="table table-bordered table-hover table-striped">
     <thead>
       <tr>
       <th>JOb ID</th>
       <th>CompanyID</th>
       <th>Job Description</th>
       <th>Duration</th>
       <th>Budget</th>
       <th>KeySkills</th>
       <th>Joining Date</th>
       <th>Expiry Date</th>
       <th>Min Experience</th>
       <th>Max Experience</th>
       <th>Status</th>
       <th>Set Status</th>
       </tr>
  </thead>
  <tbody>
<?php
$res=mysql_query("SELECT * FROM job ORDER BY jid DESC");
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><p><?php echo $row['jid']; ?></p></td>
<td><p><?php echo $row['cid']; ?></p></td>
<td><p><?php echo $row['jdesc']; ?></p></td>
<td><p><?php echo $row['duration']; ?></p></td>
<td><p><?php echo $row['budget']; ?></p></td>
<td><p><?php echo $row['keyskills']; ?></p></td>
<td><p><?php echo $row['jdate']; ?></p></td>
<td><p><?php echo $row['edate']; ?></p></td> 
<td><p><?php echo $row['cdexmin']; ?></p></td>
<td><p><?php echo $row['cdexmax']; ?></p></td>
<td><p><?php if($row['status']=="active") { echo "Active"; } else { echo "Inactive"; } ?></p></td>
<td><a href="?jid=<?php echo $row['jid']; ?>&status=<?php echo $row['status']; ?>"><?php if($row['status']=="active") { echo "Activate"; } else { echo "DeActivate"; } ?></a></td></tr>
<?php } ?>
</tbody>
</table>

Upvotes: 0

J.K
J.K

Reputation: 1374

Just modified your coding (must improve your coding)

<table class="table table-bordered table-hover table-striped">
<thead>
    <tr>
        <th>JOb ID</th>
        <th>CompanyID</th>
        <th>Job Description</th>
        <th>Duration</th>
        <th>Budget</th>
        <th>KeySkills</th>
        <th>Joining Date</th>
        <th>Expiry Date</th>
        <th>Min Experience</th>
        <th>Max Experience</th>
        <th>Status</th>
        <th>Set Status</th>        
    </tr>
</thead>
<tbody>
<?php

$res=mysql_query("SELECT * FROM job ORDER BY jid DESC");
$jid = isset($_POST['jid']) ? $_POST['jid'] : '';

while($row=mysql_fetch_array($res))
{
$num_rows = mysql_num_rows($res);

 ?>
<tr>
<td><p><?php echo $row['jid']; ?></p></td>
<td><p><?php echo $row['cid']; ?></p></td>
<td><p><?php echo $row['jdesc']; ?></p></td>
<td><p><?php echo $row['duration']; ?></p></td>
<td><p><?php echo $row['budget']; ?></p></td>
<td><p><?php echo $row['keyskills']; ?></p></td>
<td><p><?php echo $row['jdate']; ?></p></td>
<td><p><?php echo $row['edate']; ?></p></td>
<td><p><?php echo $row['cdexmin']; ?></p></td>
<td><p><?php echo $row['cdexmax']; ?></p></td>
<td><p>
<?php if($row['status']=="active")
{ 
echo "Active";
}
else {
echo "Inactive";
} ?></p></td>
<td>
  <form  action="" method="post"> 
  <input type="submit"  name="submit" value="activate"  />
  <input type="hidden"  name="jid" value="<?php echo $row['jid']; ?>"  />
<?php
if($_POST['submit']=='activate' && $row['jid']==$jid)
{
    $delet_query = mysql_query("UPDATE job SET status='active' WHERE jid='". $jid."' ") or die(mysql_error());

 if($delet_query) {
  echo 'job with id '.$row[jid].' is activated, to refresh your page, click'.  '<a href='.$_SERVER["PHP_SELF"].' > here </a>';

   }
   }
   ?>
</form>


<form action="" method="post">
<input type="submit"   name="submit"   value="deactivate"  />
<input type="hidden"  name="jid" value="<?php echo $row['jid']; ?>"  />
<?php 
if($_POST['submit']=='deactivate' && $row['jid']==$jid)
{
     $delet_query = mysql_query("UPDATE job SET status='deactive' WHERE jid='".     $jid."'  ") or die(mysql_error());

   if($delet_query) {
    echo 'job with id '.$row[jid].' is deactivated, to refresh your page, click'.       '<a href='.$_SERVER["PHP_SELF"].' > here </a>';

     }
    }
?>
</form>
</td>        
</tr>
<?php
}
?>

<tr> 
<td> <p>Total Jobs:<?php echo $num_rows; ?></p></td>
</tr>

    </tbody>
</table>

Note: The coding you need to improve. At present your display tables, updates inside while loop are messy. During the submit, the page should goes to next page, do the ncessary actions and make return to the actual page

Upvotes: 0

Ajay
Ajay

Reputation: 235

<?php
if ($_POST['submit'] == 'activate') {

    $jid = $_POST['jid'];
    $delet_query = mysql_query("UPDATE job SET status='active' WHERE jid='" . $jid . "' ") or die(mysql_error());

    if ($delet_query) {
        echo 'job with id ' . $row[jid] . ' is activated, to refresh your page, click' . '<a href=' .
            $_SERVER["PHP_SELF"] . ' > here </a>';

    }
} elseif ($_POST['submit'] == 'deactivate') {
    $jid = $_POST['jid'];
    $delet_query = mysql_query("UPDATE job SET status='deactive' WHERE jid='" . $jid . "'  ") or die(mysql_error());

    if ($delet_query) {
        echo 'job with id ' . $row[jid] . ' is deactivated, to refresh your page, click' . '<a href=' .
            $_SERVER["PHP_SELF"] . ' > here </a>';

    }
}
?>
<table class="table table-bordered table-hover table-striped">
    <thead>
    <tr>
        <th>JOb ID</th>
        <th>CompanyID</th>
        <th>Job Description</th>
        <th>Duration</th>
        <th>Budget</th>
        <th>KeySkills</th>
        <th>Joining Date</th>
        <th>Expiry Date</th>
        <th>Min Experience</th>
        <th>Max Experience</th>
        <th>Status</th>
        <th>Set Status</th>


    </tr>
    </thead>
    <tbody>
    <?php

    $res = mysql_query("SELECT * FROM job ORDER BY jid DESC");

    while ($row = mysql_fetch_array($res)) {
        $num_rows = mysql_num_rows($res);

        ?>
        <tr>
            <td><p><?php echo $row['jid']; ?></p></td>
            <td><p><?php echo $row['cid']; ?></p></td>
            <td><p><?php echo $row['jdesc']; ?></p></td>
            <td><p><?php echo $row['duration']; ?></p></td>
            <td><p><?php echo $row['budget']; ?></p></td>
            <td><p><?php echo $row['keyskills']; ?></p></td>
            <td><p><?php echo $row['jdate']; ?></p></td>
            <td><p><?php echo $row['edate']; ?></p></td>
            <td><p><?php echo $row['cdexmin']; ?></p></td>
            <td><p><?php echo $row['cdexmax']; ?></p></td>
            <td><p>
                    <?php if ($row['status'] == "active") {
                        echo "Active";
                    } else {
                        echo "Inactive";
                    } ?></p>
            </td>
            <td>

                <form action="" method="post">
                    <input type="hidden" name="jid" value="<?php echo $row['jid']; ?>"/>
                    <input type="submit" name="submit" value="activate"/>
                </form>

                <form action="" method="post">
                    <input type="hidden" name="jid" value="<?php echo $row['jid']; ?>"/>
                    <input type="submit" name="submit" value="deactivate"/>
                </form>
             </td>
        </tr>
        <?php
    }
    ?>

    <tr>
        <td><p>Total Jobs:<?php echo $num_rows; ?></p></td>
    </tr>

    </tbody>
</table>

Upvotes: 1

Related Questions