Home > Enterprise >  How do I pull specific data from an Array, and display it elsewhere? Also what am I doing wrong?
How do I pull specific data from an Array, and display it elsewhere? Also what am I doing wrong?

Time:02-21

this is my first post here, I apologize if this is in the wrong section, or has been asked before.

This is a custom cart system, its a PC Builder, you select products. Essentially I am trying to display an image from a wordpress woocommerce category. However it is a bit difficult, since the image only displays in an array, and after the image is selected.

I need to be able to select an image from a category from $item, and display the result outside of the array / main div. enter image description here Once you make a selection from "Cases" Category, I would like to display the products thumbnail in the middle (Where a Computer tower case is currently.

Here is my attempt to doing this, I have spent a few hours messing with this, and this is the only solution that has gotten me close. What am I doing wrong?

<?
if (in_array("1434", $category))
  {
    if($item > 1) {
  $wc_product = wc_get_product($item);
  echo "<img src=get_the_post_thumbnail_url($item, 'medium');"
    }
  }
else
  {
  echo "Match not found";
  }
?>
Here is the Main code for all of it.
                            <?php foreach (array_slice($categories, 0, 3) as $category){?>
                            <div >
                            <div >
                                <div >
                                <div >
<div >
                                    <div  id="item-cat-<?= esc_attr($category['id'])?>">
                                        <?php $item = isset($pc_builder[$category['id']]['id']) ? $pc_builder[$category['id']]['id'] : 0;
                                        if($item > 0): ?>
                                            <div >
                                                <?php $wc_product = wc_get_product($item);?>
                                                <a href="<?= get_permalink($item) ?>" >
                                                    <img src="<?= get_the_post_thumbnail_url($item, 'medium') ?>"
                                                         alt="<?= get_the_title($item) ?>">
                                                </a>
                                                <div >
                                                    <a href="<?= get_permalink($item) ?>" ><?= get_the_title($item) ?></a>
                                                </div>
                                                <div >
                                                    <div  data-price="<?= esc_attr($wc_product->get_price())?>"><?= wc_price($wc_product->get_price()) ?></div>
                                                    <div >
                                                        <input type="number" data-value="<?= esc_attr($category['id']) ?>" value="<?=  esc_attr($pc_builder[$category['id']]['quantity']) ?>"  min="1">
                                                    </div>
                                                    <i> = </i>
                                                    <div  data-price="<?= esc_attr($wc_product->get_price())?>"><?= wc_price($wc_product->get_price()*(float)$pc_builder[$category['id']]['quantity']) ?>
                                                    </div>
                                                </div>
                                                <div >
                                                    <button data-toggle="nk-popup"  data-id="<?= esc_attr($category['id'])?>"><i ></i></button>
                                                    <button  data-cat_id="<?= esc_attr($category['id'])?>" data-product_id="<?= esc_attr($item)?>"><i ></i></button>
                                                </div>


                                            </div>
                                            
                                        <?php else:?>
                                            <button data-toggle="nk-popup"  data-id="<?= esc_attr($category['id'])?>"><i ></i> <span style="display: none;"><?= esc_html(__('Select','nk-custom-pc-builder'))?> <?= esc_attr($category['title'])?></span></button>
                                        <?php endif;?>
                                    </div></div>
                                    <div >
                                                                        <div >
                                                                        
                                        <?= esc_html($category['title'])?>
                                    </div>
                                    </div>
                                </div></div></div></div>
                            <?php }?>

CodePudding user response:

Without testing your code, to me it looks like you have the echo statement wrong. Here is how I would do it:

<?
if (in_array("1434", $category)) {
  if($item > 1) {
    $wc_product = wc_get_product($item);
    echo '<img src="' . get_the_post_thumbnail_url($item, 'medium') . '">'; 
  }
} else {
    echo "Match not found";
}
?>
  • Related