AJB
AJB

Reputation: 51

Attempting to make my own while Loop just like Wordpress Loop

I recently read this article: How to make my own while Loop just like Wordpress Loop? and I am trying to make the loop myself without any success so far.

The answers given in the article recommend an OOP approach as opposed to using global variables.

I have had no luck with the OOP method but the global variable method below almost works.

Instead of displaying 'item_title' and 'item_desc' values from MySQL table, letters appear. Note that the letters are in the correct while-loop format.

What am I doing wrong?

Many thanks

<?php
//CONNECT TO DATABASE
$mysqli = mysqli_connect("localhost", "username", "password", "testDB");

//VALIDATE ITEMS FROM STORE_ITEMS TABLE
$get_item_sql = "SELECT * FROM store_items";
$get_item_res = mysqli_query($mysqli, $get_item_sql) or die(mysqli_error($mysqli));

//DEFINE VARIABLES
$posts = mysqli_fetch_array($get_item_res);
$post = null;
$post_count = 0;
$post_index = 0;

//HAVE_POST FUNCTION
function have_post() {
global $posts, $post_count, $post_index;

if ($posts && ($post_index <= $post_count)){
    $post_count = count($posts);
    return true;
}
else {
    $post_count = 0;
    return false;
}
}

//THE_POST FUNCTION
function the_post() {
global $posts, $post, $post_count, $post_index;

// make sure all the posts haven't already been looped through
if ($post_index > $post_count) {
    return false;
}

// retrieve the post data for the current index
$post = $posts[$post_index+1];

// increment the index for the next time this method is called
$post_index++;
return $post;

}

//THE_TITLE FUNCTION
function the_title() {
global $post;
return $post['item_title'];
}

//THE_CONTENT FUNCTION
function the_content() {
global $post;
return $post['item_desc'];
}

//OUTPUT
if(have_post()) : while(have_post()) : the_post();
echo '<h2>'.the_title().'</h2>';
echo '<p>'.the_content().'</p>';
endwhile; endif;

?>

Upvotes: 0

Views: 368

Answers (2)

Welling
Welling

Reputation: 556

as i can see you are just querying one row by using

$posts = mysqli_fetch_array($get_item_res);

you need to fill one array with all thoses rows, this way.

$posts = array();
while ($row  = mysqli_fetch_array($get_item_res) ){
    $posts[] = $row;
}

Upvotes: 0

Marc B
Marc B

Reputation: 360572

You're doing MySQL wrong. mysqli_fetch_array fetches a single ROW of data. It doesn't retrieve all rows in the query result. Your query is also inefficient. If you want just a count of how many posts there are, you can do

$result = mysqli_query("SELECT * FROM ...");
$rows = mysqli_num_rows($result);

but that's inefficient - you're forcing the DB library to start fetching row data on the assumption you're actually going to be using it. Yet you're just throwing it away. A better way is

$result = mysqli_query("SELECT count(*) AS cnt FROM ...") or die(mysqli_error());
$row = mysqli_fetch_assoc($result);
$rows = $row['cnt'];

Later on you then treat $posts as if it did contain all of the query results, but since it contains only that one single row, you're simply iterating/fetching the fields of that one row.

Upvotes: 1

Related Questions