0

Searched for the solution- found nothing. Maybe I used the wrong terms.

I try to split the server-side operations and the html structure. So I made two different files, first the index.php and then operation.php.

Inside of the operation.php I made a mysqli query and fetched the content of an mysql table into different variables:

$query = "SELECT * FROM work";
    if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $title = $row["title"]; 
        $desc = $row["desc"];
        $id = $row["id"];
        $date = $row["date"];
        $time = $row["time"];
        $kat = $row["kat"];
        $user = $row["user"];
    }
    $result->free();
    }
$mysqli->close();
?>

Now I tried to use this in the index.php by including the operation.php and using it like

<div><?php echo $title ?></div>

Everything works, but I want to loop this div and get one <div> for every 'title' row in the database.

How is this possible?

Thanks in advance.

3
  • Format the output in operation.php unless you want to retrieve all of the data as an array and then loop while formatting. Commented Oct 17, 2014 at 13:03
  • I thought it would be very comfortable to split the php and html- so I wanted to avoid html in operation.php. Edit: But I will change my mind, if this way is better! Commented Oct 17, 2014 at 13:07
  • I agree, for view you will have to have some processing. We do this all of the time - store the results of the query in an object and then loop through that object in your index.php. Really you shouldn't loop in operation.php, just return the results. Then loop through the results in index.php. It would be much cleaner and your code will be reusable. Commented Oct 17, 2014 at 13:09

1 Answer 1

1

Well, you could declare an array at the top

$rows = array();
...
while ($row = $result->fetch_assoc()) {
    $rows[] = $row;
}
....

foreach($rows as $row) {
    echo '<div>' . $row['title'] . '</div>';
}

That could be a solution.

On the other hand, if you want to separate the logic and the view, you could take a look at MVC design patterns, or use a templating engine (like Twig for example)

What a templating engine allows you to do is something like this (not tested ;-)

$variables = array();
while ($row = $result->fetch_assoc()) {
    $variables['rows'][] = $row;
}
$twig->render('index.html.twig', $variables);

And in the separate index.html.twig you can use the templating language

...
<body>
{% for row in rows %}
    <div>{{ row.title }}</div>
{% endfor %}
</body>

I hope this helps :)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I´m new to php and tried to use your code- but I get an error, that the server couldn´t fetch the mysqli result. Where is the mistake? What did I wrong?
I used parts of your code to illustrate another path you could take. Next to that, you need the Twig library for the templating to work. If you are new to PHP, programming the bare basics can be very rewarding, but also have a high learning curve. I would suggest that you start out with a framework which does all those things for you. Populair frameworks are for example Symfony, CakePHP, Kohana, Laravel, etcetera. These frameworks ususally have great tutorials on how to get going. laravel.com/docs/4.2/quick kohanaframework.org/3.3/guide/kohana/install etcetera
When you get the hang of using a framework, you can look into the code actually running the framework and look at how they solved specific problems, and learn from that, create your own framework modules, work on bugs in the framework project, etcetera. Also, I would highly recommend joining a PHP user group. I see you are from stuttgart, I found this one : phpugs.de Usually there are people from different experience levels there. Follow a talk, talk to some people, this usually can give you a kickstart into PHP and programming in general.
I'll counter @Dennis's argument :). Time spent developing from scratch gives you the tools to troubleshoot when something goes wrong in a bulky, bloated, high-overhead framework. Does the framework simplify things? Yes, especially for large websites that grow and change often. For smaller site I would never use anything as heavy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.