Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 产品比较会议_Php_Jquery_Ajax_Session - Fatal编程技术网

Php 产品比较会议

Php 产品比较会议,php,jquery,ajax,session,Php,Jquery,Ajax,Session,我正在从事一个项目,需要能够比较2个或更多的产品。客户端需要一个能够直接链接到此类比较的页面 我选择的实现方式是使用URL,比如:/compare?id=5-20-100(用于比较id为5、20和100的产品) 基本上,整个过程都通过以下脚本进行解析: $list = explode( '-', $_GET['id'] ); $myarray = $list; $args = array( 'post_type' => 'products', 'post__in'

我正在从事一个项目,需要能够比较2个或更多的产品。客户端需要一个能够直接链接到此类比较的页面

我选择的实现方式是使用URL,比如:
/compare?id=5-20-100
(用于比较id为5、20和100的产品)

基本上,整个过程都通过以下脚本进行解析:

$list = explode( '-', $_GET['id'] );

$myarray = $list;

$args = array(
   'post_type' => 'products',
   'post__in'      => $myarray
);
// The Query
$the_query = new WP_Query( $args );
为了将产品添加到列表中,我打算使用一个会话,其中项目通过AJAX添加(我使用的是jQuery,这很重要)。所以,我遇到的问题是:

当用户单击“比较此产品”时,我需要获取此产品的ID(可能来自html数据属性
product ID=“100”

如何设置此AJAX会话?更准确地说:当用户点击“比较项目”按钮时,我如何让它们进入
/compare?id=5-20-100
URL

前端更新:

<a href="#" data-id="5">Product 1 add to compare</a>
<a href="#" data-id="20">Product 2 add to compare</a>

查看比较产品,在这一部分中,我需要从ajax会话获取url

<a href="compare?id=5-20">View compare products</a>

我的建议是使用带有数组访问符号的复选框输入

<form id="compare" method="get" action="/path/to/compare.php" enctype="application/x-www-form-urlencoded">
<input type="checkbox" name="compare[]" value="[ID POPULATED VIA PHP]" />
<input type="checkbox" name="compare[]" value="[ID POPULATED VIA PHP]" />
<input type="checkbox" name="compare[]" value="[ID POPULATED VIA PHP]" />
...
<input type="submit" value="Compare Selected Items" />
</form>
您可以轻松访问
$\u GET['compare']
数组中选择的所有比较id。自从使用GET之后,您还可以使用书签URL


这也可以在不需要javascript的情况下工作(当然,我们仍然可以添加javascript表单验证-也许是为了确保在使用比较之前至少选中两个复选框。)

首先,收集id并附加到链接不需要使用AJAX

您想要的解决方案需要在链接上设置eventhandler。使用Javascript而不使用JQuery是很理想的,但是由于您已经在使用它并讨论数据属性,您可以在链接上设置EventHandler,然后使用JQuery
data()
方法获取id,最后将其附加到最后一个链接上的href属性。你可以读到它


更好的解决方案是使用表单@Mike Brant as刚刚提交了这样一个解决方案,我建议您使用。

这里是最后一个代码,用于书签、比较或添加购物车基本功能:

JS:

Wordpress模板:

<?php

$list = explode( '-', $_GET['id'] );

$myarray = $list;

$args = array(
   'post_type' => 'products',
   'post__in'      => $myarray
);
// The Query
$the_query = new WP_Query( $args );

?>

<?php if ( $the_query->have_posts() ) : ?>

  <!-- pagination here -->

  <!-- the loop -->
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <h2><?php the_title(); ?></h2>
  <?php endwhile; ?>
  <!-- end of the loop -->

HTML按钮:

<a href="http://localhost/auto/cart/?id=24-40" class="view_compare">Show compare products</a>
<a href="#" class="aaddtocart" data-compare="<?php echo get_the_ID(); ?>">Add to compare list</a>


是,wordpress的一部分。我做了一个自定义循环。在循环页面上,我列出了产品的所有信息,然后制作了一个排序数据的表视图。它是在前端还是后端?你能添加一些你到目前为止所做的代码吗?这是后端部分,在后端是没有问题的,问题是为获取数据的比较页面设置一个ids筛选。所以,只是为了确保我得到了正确的答案。您需要帮助的部分是从用户单击要比较的内容创建比较?id=x链接?是,检查问题,前端更新。例如,包含50个产品的产品列表,每个产品上都有一个按钮“添加到比较”,以及最后一个按钮“链接”和“查看比较产品”。但是,如果“查看比较产品”位于其他页面上,而不是在同一页面上,则在哪里找不到产品?例如:products.php-带有按钮的产品列表,比较。另一个页面是ex.catalog.php,或者主整体标题,在这里我们需要放置一个“查看比较产品”。您的解决方案适用于单页查看。也许用饼干?存储、创建、更新、删除cookies?@Foxsk8获取端点是单个页面还是另一个页面并不重要。选中复选框的所有值都将传入
$\u GET['compare']
。在这种情况下,您只需在复选框中将产品id设置为
属性即可。当您进入比较页面时,所有选中的产品id值都将在
$\u get['compare']
中可用。问题已解决。我需要使用Cookie,对于后者,在全局按钮“比较产品”上获取比较url,其中所有发布的获取ID都存储在Cookie上,以便以后在所有站点上使用。前端部分设置全局删除器使用url时出现问题。:)
<?php

$list = explode( '-', $_GET['id'] );

$myarray = $list;

$args = array(
   'post_type' => 'products',
   'post__in'      => $myarray
);
// The Query
$the_query = new WP_Query( $args );

?>

<?php if ( $the_query->have_posts() ) : ?>

  <!-- pagination here -->

  <!-- the loop -->
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <h2><?php the_title(); ?></h2>
  <?php endwhile; ?>
  <!-- end of the loop -->
<a href="http://localhost/auto/cart/?id=24-40" class="view_compare">Show compare products</a>
<a href="#" class="aaddtocart" data-compare="<?php echo get_the_ID(); ?>">Add to compare list</a>