Set a variable in url using php and mysql

I need to set two variables in the URL, $ id and $ job. Data must come from the select mysql statement, [id] and [job_number_id]. My page displays a job offer of a client, and if several offers are displayed, but [id] and [job_number_id] determine what is displayed on the web page. I do not know how to do that. Any help would be greatly appreciated. Here is the code:

<?php

$url = 'http://localhost/estimate_lar/homepage.php';
$id = $_SESSION['id'];

$query =    "SELECT id, client_job_name, job_number_id
            FROM `job_name`
            WHERE `id`='$id'";
$allJobs = $db->query($query);

?>

<?php foreach ($allJobs as $site_title) : ?>
 <p> 
  <tr><?php echo '<a href="'.$url.'">'.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.'</a>'; ?>
    <td></td>
  </tr>
</p>
<?php endforeach; ?>
+4
source share
2 answers

If you want the variables to be available in the URL, you need to read them with $_GET.

URL-, index.php?id=1&job_number_id=3, :

if (isset($_GET['id']) && isset($_GET['job_number_id'])) {//make sure both arguments are set
    $id = $_GET['id'];
    $job_number_id = $_GET['job_number_id'];
}

foreach:

<?php foreach ($allJobs as $site_title) : ?>
 <p> 
      <tr><?php 

$url = "http://localhost/estimate_lar/homepage.php?id=" . $site_title['id'] . "&job_number_id=" . $site_title['job_number_id'];

echo '<a href="'.$url.'">'.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.'</a>'; 

    ?>
    <td></td>
  </tr>
</p>
<?php endforeach; ?>

, SQL- , . - .

script , URL- .

, !

+3

.

<?php
session_start();
$id = $_SESSION['id'];
$url = 'http://localhost/estimate_lar/homepage.php';
if($id){

$query =    "SELECT id, client_job_name, job_number_id  FROM `job_name`  WHERE `id`='$id'";
$allJobs = $db->query($query);

}else{
echo "Id not in session";
}
?>
 <table>
<?php if ($allJobs)  { foreach ($allJobs as $site_title) : ?>

  <tr>
  <td>
 <a href="<?php echo $url; ?>?client_job_name=<?php echo $site_title['client_job_name'];?>&job_number_id=<?php echo $site_title['job_number_id'];?> "> <?php echo $site_title['job_number_id']. " ".$site_title['job_number_id']; ?></a>
  </td>

 </tr>

<?php endforeach; ?>
</table>

 <?php }else{ echo 'No results Found' ;} ?>

.

0

Source: https://habr.com/ru/post/1524228/


All Articles