douliu7929
2016-02-09 17:02
浏览 27
已采纳

PHP - 更新以前的cookie以形成数组

I have the following code:-

<?php 

if(!is_user_logged_in()) {
    $fav_cookie = array($_GET['job_fav']);
    $cookie_url = '/jobs/?job_fav=<?php the_ID(); ?>';

    if($job_fav !='') {
         setcookie(COOKIE_PREFIX . "job_fav",    " ", time() - 3600);
         setcookie(COOKIE_PREFIX . "job_fav", array($fav_cookie));
         header("Location: $cookie_url");
    }
?>

<a href="/jobs/?job_fav=<?php the_ID(); ?>">
    <div class="job-single-favourite icon-<?php echo $job_sector_html; ?>
        <?php if (in_array(get_the_ID(), $fav_cookie)) {
                  echo ' starred-job';
              }
        ?>">
    </div>
</a><!-- none-logged-in starred-job -->

<?php } ?>

This is working fine, but it is only storing one value as a cookie at a time.

Basically what I am wanting to do is on click of the favourite icon it needs to store the job ID within the $fav_cookie array.

What is happening at the moment is that if you favourite a job $fav_cookie is being replaced by the new job ID. I somehow want to add each cookie inside the $fav_cookie array so it would become 113, 120, 234 for example, instead of replacing the ID each time.

If I haven't explained what I am wanting to achieve in great enough detail, let me know and I will amend my post.

EDIT:--

Just to add, the following works:-

$jobID = get_the_ID();
$jobListing = array(get_the_ID());
$html = '';
//$currentIDs = explode('|', $_COOKIE[COOKIE_PREFIX . 'job_fav']);
$currentIDs = array('493','311');

var_dump($currentIDs);
foreach ($jobListing as $job) {

    $customClass = (in_array($jobID, $currentIDs)) ? ' starred-job' : '';

   // $html = '';
    $html .= '<a href="/jobs/?job_fav=' . htmlspecialchars($jobID) . '">';
    $html .= '<div class="job-single-favourite icon-' . htmlspecialchars($job_sector_html) . $customClass . '">';
    $html .= '</div></a>';
}


echo $html;

All I need now is $currentIDs to display be an array of the favourite jobs defined from $_COOKIE and it should be done.

$currentIDs = explode('|', $_COOKIE[COOKIE_PREFIX . 'job_fav']); is an empty array

EDIT 2

Okay I now have the following:-

$jobID = get_the_ID();
$jobListing = array(get_the_ID());

$favourite_cookie = array($_GET['job_fav']);
$ids_string = implode('|', $favourite_cookie);
setcookie('job_fav', $ids_string);

var_dump($_COOKIE['job_fav']);


$ids_string = $_COOKIE['job_fav'];
$ids = explode('|', $_COOKIE['job_fav']);
<?php 
if (!is_user_logged_in()) {

$jobID = get_the_ID();
$jobListing = array(get_the_ID());
$html = '';
$currentIDs = explode('|', $_COOKIE[COOKIE_PREFIX . 'job_fav']);
$currentIDs = $ids;

foreach ($jobListing as $job) {

    $customClass = (in_array($jobID, $currentIDs)) ? ' starred-job' : '';

    $html = '';
    $html .= '<a href="/jobs/?job_fav=' . htmlspecialchars($jobID) . '">';
    $html .= '<div class="job-single-favourite icon-' . htmlspecialchars($job_sector_html) . $customClass . '">';
    $html .= '</div></a>';
}


echo $html;

} ?>

And now have two issues:-

  • When I favourite a job, I have to click twice in order for it to update.
  • $_COOKIE['job_fav'] gets replaced with the new job ID every time you favourite a different job, instead of adding it to the array
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题