skip to Main Content

(I want it like this) For Example. I dont know how to create 2d arrays where i can add section title and show several packages after that i need to show logo design heading with logo design packages and etc..

<section id="packages" data-aos-delay="0" data-aos-duration="0" data-aos="fade" class="page-section">

    <div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
      <h1 class="display-4" data-aos="zoom-out-up">Packages</h1>
    </div>

    <div class="container">
      <div class="row justify-content-center mb-3">
        <?php foreach ($temp_packages as $package) : ?>
          <?php
          $title = $package['title'];
          $package_price = get_post_meta($package['id'], '_package_price', true);
          $package_subtitle = get_post_meta($package['id'], '_package_subtitle', true);
          $package_attributes = get_post_meta($package['id'], "_package_attributes", true);
          ?>
          <div class="col-lg-4 col-md-4 col-sm-12">
            <div class="card mb-4 box-shadow">
              <div class="card-header">
                <h2 class="my-3">$ <?php echo $package_price; ?></h2>
                <h4 class="my-1"><?php echo $title; ?></h4>
                <h6 class="mb-2"><?php echo $package_subtitle; ?></h6>
              </div>
              <div class="card-body">
                <ul class="list-unstyled">
                  <?php if (gettype($package_attributes) == 'array') : ?>
                    <?php foreach ($package_attributes as $attribute) : ?>
                      <li>
                        
                      </li>
                    <?php endforeach; ?>
                  <?php endif; ?>
                </ul>
                <a href="#contactUs" type="button" class="btn btn-lg btn-block btn-primary">Enquire</a>
              </div>
            </div>
          </div>
        <?php endforeach; ?>
      </div>
    </div>
  </section>
<?php endif; ?>

2

Answers


  1. Creating 2D arrays in PHP is actually very easy:

    <?php
    
    $myArray = [
       [
          "title" => "foo",
          "desc" => "bar"
       ],
       [
          "title" => "foo2",
          "desc" => "bar2"
       ],
       [
          "title" => "foo3",
          "desc" => "bar3"
       ],
       [
          "title" => "foo4",
          "desc" => "bar4"
       ]
    ];
    
    echo $myArray[0]["title"]; // foo
    
    foreach($myArray as $value) {
       echo $value["title"];
    }
    
    // foo foo2 foo3 foo4
    

    Is that what you were looking for?

    Login or Signup to reply.
  2. I just created an Array PHP: Arrays with all the content you need. Here is my array with one example entry:

    $temp_packages = Array(
        [
            'id' => 12345,
            'title' => 'Some Title',
            'subtitle' => 'Some Subtitle',
            'price' => 129.99,
            'attributes' => [
                'An Attribute',
                'Some more Stuff',
                'foo',
                'bar'
            ]
        ],
    );
    

    The content of $temp_packages will probaly be stored in a database and be fetched from there.

    After that, you can use your code to generate your section. I changed a bit, so it works for me:

    <section id="packages" data-aos-delay="0" data-aos-duration="0" data-aos="fade" class="page-section">
    <div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
        <h1 class="display-4" data-aos="zoom-out-up">Packages</h1>
    </div>
    <div class="container">
        <div class="row justify-content-center mb-3">
        <?php foreach ($temp_packages as $package) : ?>
            <div class="col-lg-4 col-md-4 col-sm-12">
            <div class="card mb-4 box-shadow">
                <div class="card-header">
                <h2 class="my-3">$ <?php echo $package['price']; ?></h2>
                <h4 class="my-1"><?php echo $package['title']; ?></h4>
                <h6 class="mb-2"><?php echo  $package['subtitle']; ?></h6>
                </div>
                <div class="card-body">
                <ul class="list-unstyled">
                    <?php if (gettype($package['attributes']) == 'array') : ?>
                    <?php foreach ($package['attributes'] as $attribute) : ?>
                        <li>
                        <?php echo $attribute; ?>
                        </li>
                    <?php endforeach; ?>
                    <?php endif; ?>
                    </ul>
                <a href="#contactUs" type="button" class="btn btn-lg btn-block btn-primary">Enquire</a>
                </div>
            </div>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
    </section>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search