A
A
Alexey Smirnov2017-11-21 11:28:40
Java
Alexey Smirnov, 2017-11-21 11:28:40

What is the correct way to use a cycle?

Good day.
There are goods, they are in the MySql database. There are also divs with different classes. They need to put the goods that I take from the database.
How can I make it loop so that each product goes into these divs. Here's what I roughly mean:

$result = mysql_query("SELECT * FROM `catalog` WHERE `type`='$_GET[type]' ORDER BY `id` DESC"); 
$res = array();
// знаю, что $_GET нужно обрабатывать, это просто пример

And here are the containers in which you need to put data from the $res arrays
<div class="item active">Товар 1</div>
<div class="item active big">Товар 2</div>
<div class="item active">Товар 3</div>
<div class="item active right">Товар 4</div>
<div class="item active margin-top-500">Товар 5</div>

How to write a loop correctly so that all products are filled and there are no extra (empty) div containers left?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey Gornostaev, 2019-09-02
@DARKENN

Starting from the 3rd version, the signature of the constructor has changed. Either rollback to previous versions, or change the code to something like:

FirebaseListOptions<Message> options =
    new FirebaseListOptions.Builder<Message>()
        .setQuery(mDatabase.child("chat"), Message.class)
        .setLayout(R.layout.item)
        .build();
adapter = new FirebaseListAdapter<Message>(options) {
...

I
Ilya Serov, 2019-09-02
@JavaIlya

I could be wrong, but my first argument is that you put this ---> com.example.parallax.Game.Chat in there, and the constructor expects com.example.parallax.Game.Message

I
Ivan Koryukov, 2017-11-21
@MadridianFox

You should not immediately write a certain number of elements, and then stuff the result into them. No.
You should in a loop immediately display the markup into which you insert data. How many iterations the loop will make - exactly so many tags will be eaten out.
The example below is terrible, but it gets the point across.)

$result = mysql_query("SELECT * FROM `catalog` WHERE `type`='$_GET[type]' ORDER BY `id` DESC");
while($row = mysql_fetch_assoc($result)){
    echo "<div class=\"item active\">".$row["name"]."</div>";
}

Z
zoozag, 2017-11-21
@zoozag

If these classes are not data dependent, use the nth-child selector in css instead

J
John Doe, 2017-11-21
@rabbit418

Better use PDO.

<?php
# подключаемся к базе данных  
try {
  $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}catch(PDOException $e) {
  echo "Хьюстон, у нас проблемы.";
}

# данные, которые мы вставляем  
$data = [
  'type' => $_GET['type']
];

# делаем запрос
$STH = $DBH->prepare("select * from catalog where catalog.type = :type order by catalog.id desc");
$STH->execute($data);

# устанавливаем режим выборки
$STH->setFetchMode(PDO::FETCH_ASSOC);

# выводим результат
while($row = $STH->fetch()) {
  echo "<div class=\"item active\">{$row['name']} - {$row['price']}</div>";
}
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question