K
K
Katsuro2018-11-20 12:47:30
PHP
Katsuro, 2018-11-20 12:47:30

How to link two conditions in foreach php?

<?php  include("base.php"); 

    //Page

    $page = isset( $_GET['page'] ) ? (int)$_GET['page'] : 1;
    $prePage = isset( $_GET['pre-page'] ) && $_GET['pre-page'] <= 50 ? (int)$_GET['pre-page'] : 4;

    //Positioning
    $start = ($page > 1) ? ($page * $prePage) - $prePage : 0;

    //Query
    $row = $pdo->prepare(" SELECT SQL_CALC_FOUND_ROWS id, name , payback , `energy-consumption` , hashrate , income , price  FROM `items` LIMIT {$start}, {$prePage} ");

    $row->execute();

    $row = $row->fetchAll(PDO::FETCH_ASSOC);

    // $row = $row->fetchAll(PDO::FETCH_ASSOC);

    //Pages
    $total = $pdo->query( "SELECT FOUND_ROWS() as total")->fetch()['total'];
    $pages = ceil($total / $prePage);



    $q = "SELECT * FROM `items` ORDER BY `items`.`price`";
    // echo dclean($_GET['search']);
    if ($_GET['search']) {
       $search = $_GET['search'];
       $q = "SELECT * FROM `items` WHERE `name` LIKE '%$search%' ";
    }
    if ($_GET['sort_by']=='ASC') {
        $q = "SELECT * FROM `items` ORDER BY `items`.`price` ASC";
    }
    if ($_GET['sort_by']=='DESC') {
        $q = "SELECT * FROM `items` ORDER BY `items`.`price` DESC";
    }

?><code lang="html">
<!--  <?php //<code lang="php">
foreach($pdo->query($q) as $row )
</code>{ ?> -->
                    <?php foreach($row as $row ){ ?>
                        <div class="col-lg-6 col-md-6">
                            <div class="product">
                                <div class="product-photo">
                                    <img src="img/product/1.png" alt="Product">
                                </div>
                                <div class="product-content">
                                    <div class="product-content-header">
                                        <h2><?php echo $row['name']; ?></h2>
                                    </div>
                                    <div class="product-content-characteristic">
                                        <h4>Окупаемость:  <span><?php echo $row['payback']; ?></span><br>
                                        Энергопотребление:  <span><?php echo $row['energy-consumption']; ?></span><br>
                                        Хэшрейт:  <span><?php echo $row['hashrate']; ?></span></h4>
                                    </div>
                                    <div class="product-content-price">
                                        <div class="income">
                                            <p>Доход в месяц:</p>
                                            <h1>= <?php echo $row['income']; ?>p.</h1>
                                        </div>
                                        <div class="cost">
                                            <p>Стоимость оборудования</p>
                                            <h1>= <?php echo $row['price']; ?>p.</h1>
                                        </div>
                                    </div>
                                    <div class="product-content-button">
                                        <a class="btn btn-product">Приобрести оборудование</a>
                                    </div>
                                </div>
                            </div> 
                        </div>
                    <?php }; ?>
</code>
foreach($pdo->query($q) as $row ) //это условия действует на сортировку и поиск 
foreach($row as $row ) // это условия для пагинации

I need sorting and searching for products and pagination to work together, but I can't link the two functions together, can anyone tell me?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-11-20
@Stalker_RED

For pagination, use limit/offset in the SQL query and you won't need to combine anything.
And foreach is not a condition, but a language construct for iterating over an array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question