Looping a Function like Wordpress have_posts - php

I am trying to loop a function through while similar to WordPress. I use the function to return a boolean of true or false. Now here is my function. Now, with it only returning true or false, I know that I have to get my function has_rows(); to change to false after it looks through all my rows. Is this possible?
So I guess my question is if it is possible, and how, to loop the a function in a while loop like the below example. I know it is possible because wordpress uses a function, my question is how.
THIS is what I have tried. And the loop wont stop.
function has_rows () {
global $Q_INFO;
global $DB_CONN;
static $COUNT; // STATIC TO REMEMBER LAST TIME FUNCTION CALLED
$COUNT = #$COUNT ? $COUNT++ : 0; // IF NOT SET, SET TO 0 AND COUNT EACH LOOP
echo $QUERY = "SELECT * FROM `" . $Q_INFO['table'] . "` LIMIT " . $COUNT . ", 1";
$STMT = $DB_CONN->prepare($QUERY);
$STMT->execute();
$RESULT = $STMT->get_result();
$STMT->close();
if ($RESULT->num_rows > 0) {
return true;
} else {
return false;
}
}
Im calling it just like in wordpress:
<?php if (has_rows()):?>
<?php while (has_rows()): ?>
Hi World!!
<?php endwhile; ?>
<?php endif; ?>
HERE IS MY FINAL CODE:
I had to add another static variable for the very first call of the function in if(has_rows()): and then I used the selected answer below to do the rest.
Thanks for the help guys!
function has_rows () {
global $Q_INFO;
global $DB_CONN;
static $I = 0;
static $COUNT = 0;
if ($I == 0) {
$QUERY = "SELECT * FROM `" . $Q_INFO['table'] . "`";
$I++;
} else {
$QUERY = "SELECT * FROM `" . $Q_INFO['table'] . "` LIMIT " . $COUNT . ", 1";
$COUNT++;
}
$STMT = $DB_CONN->prepare($QUERY);
$STMT->execute();
$RESULT = $STMT->get_result();
$STMT->close();
if ($I != 0) {
while ($ROW = $RESULT->fetch_assoc()) {
foreach ($ROW as $KEY=>$VALUE) {
$Q_INFO[$KEY] = $VALUE;
}
}
}
if ($RESULT->num_rows > 0) {
return true;
} else {
return false;
}
}

The first time you run it, $COUNT is 0, which evaluates to boolean false. So, $COUNT = #$COUNT ? $COUNT++ : 0; sets it to 0 again.
Just take out that line, explicitly initialize $COUNT to 0, and put $COUNT++; right before your final if statement.
function has_rows () {
global $Q_INFO;
global $DB_CONN;
static $COUNT = 0; // STATIC TO REMEMBER LAST TIME FUNCTION CALLED
// Other code here...
$COUNT++;
if ($RESULT->num_rows > 0) {
return true;
} else {
return false;
}
}

If your trying just to walk over an array, like the wordpress does, try this:
$posts = array('post1', 'post2', 'post3');
function has_posts()
{
// this line will make sure that you have a non empty array
if ( ! is_array($posts) or ! count($posts)) return false;
else return each($posts);
}
while ($post = has_posts())
{
echo $post;
}

Related

After adding pagination PHP function no longer displays mysql array data

I have made a PHP function that pulls data from a table (product_reviews) in my database, showing all reviews for a unique product_id. Here is the code:
function showReviews ($product_id)
{
include('database_conn.php');
$query = "SELECT * FROM product_reviews WHERE product_reviews.productID='".$product_id."'";
if ($queryresult = mysqli_query($conn, $query))
{
while ($currentrow = mysqli_fetch_assoc($queryresult))
{
$result_list[] = $currentrow;
}
foreach ($result_list as $currentrow)
{
$productitems[] = array(
$customer_forename = $currentrow['customer_forename'],
$customer_surname = $currentrow['customer_surname'],
$review_title = $currentrow['review_title'],
$review_text = $currentrow['review_text']);
echo '<article class="Review_Box">';
echo "<h3>".$review_title." by ".$customer_forename." ".$customer_surname."</h3></br>";
echo "<p>".$review_text."</p>";
echo "</article>";
}
}
}
This function is then called from the products page and it works as intended.
But when I add pagination to the function ( following an ehow tutorial), the function doesn't have any output(see here).
if(!function_exists('showReviews'))
{
function showReviews ($product_id)
{
include('database_conn.php');
include('functions.php');
$rowsPerPage = 3;
$currentPage = ((isset($_GET['page']) && $_GET['page'] > 0) ? (int)$_GET['page'] : 1);
$offset = ($currentPage-1)*$rowsPerPage;
$query = "SELECT * FROM product_reviews WHERE product_reviews.productID='".$product_id."' LIMIT '".$offset."','".$rowsPerPage."'";
if ($queryresult = mysqli_query($conn, $query))
{
while ($currentrow = mysqli_fetch_assoc($queryresult))
{
$result_list[] = $currentrow;
}
foreach ($result_list as $currentrow)
{
$productitems[] = array(
$customer_forename = $currentrow['customer_forename'],
$customer_surname = $currentrow['customer_surname'],
$review_title = $currentrow['review_title'],
$review_text = $currentrow['review_text']);
echo '<article class="Review_Box">';
echo "<h3>".$review_title." by ".$customer_forename." ".$customer_surname."</h3></br>";
echo "<p>".$review_text."</p>";
echo "</article>";
}
}
$count = countReviews (1);
$totalPages = $count/$rowsPerPage;
if($currentPage > 1)
{
echo 'Previous Page ';
}
if($currentPage < $totalPages)
{
echo 'Next Page';
}
}
}
I tested my sql query and it works fine in mysql Workbench.
What am I doing wrong? Can anyone recommend a better way to do this?
When you create your query, you are encapsulating the offset and limit between single quotes (if I read your code correctly).
SELECT * FROM product_reviews WHERE product_reviews.productID='25' LIMIT '0','3'
Try removing those single quotes.

return array in function

I want to translate each words in array:
$myarray = array("hi","bro");
So I wrote a translate function like this:
function translate($word) {
foreach ( $word as $key_translate ) {
$array = array();
$sql = mysql_query("SELECT * FROM `translate` WHERE name = '".$key_translate."' ");
if ( mysql_num_rows($sql) == 1 ) {
$row = mysql_fetch_array($sql);
$name = $row['fa_name'];
return $name;
//retuen array($name);
}
else {
return $key_translate;
//return array($key_translate);
}
}
}
And using this to show translated array:
print_r (translate($myarray));
But it's not returning array, it's just showing first key as string.
How can I return array in function?
Don't return inside the loop, that exits the whole function immediately and just returns that one element. Accumulate the results in an array, and return that.
function translate($word) {
$result = array();
foreach ( $word as $key_translate ) {
$sql = mysql_query("SELECT fa_name FROM `translate` WHERE name = '".$key_translate."' ");
if ( mysql_num_rows($sql) == 1 ) {
$row = mysql_fetch_array($sql);
$result[] = $row['fa_name'];
}
else {
$result[] = $key_translate;
}
}
return $result;
}
Also, there's no reason to use SELECT * if you're only interested in fa_name.
Try this:
function translate($word) {
foreach ($word as $key => $translate) {
$sql = mysql_query("SELECT * FROM `translate` WHERE name = '" . $translate . "' ");
if (mysql_num_rows($sql) == 1) {
$row = mysql_fetch_array($sql);
$word[$key] = $row['fa_name'];
}
}
return $word;
}

MySQLi Rows Not Loading

i have a basic form that loads 15 dropdown boxes with the same topics in each box. this is a voting page where the user can vote for his favorite topic or his least favorite topic. the problem i have is that the topics arent being loaded when i tell them to. Here is my code.
PHP
<?php
$Vote = new Vote();
class Vote {
public function GetTopic() {
$Connect = new mysqli("127.0.0.1", "root", "", "Data");
$Query = 'SELECT * FROM Topics';
if($Gather = $Connect->query($Query))
{
while($Row = $Gather->fetch_assoc())
{
$Topic = $Row['Topic'];
echo '<option>'.$Topic.'</option>';
}
$Gather->free();
}
else
{
echo 'Error';
}
$Connect->close();
}
public function LoadTopic() {
for($I = 15; $I > 0; $I--)
{
echo '<select><option>'.$I.'</option>'.$this->GetTopic().'</select>';
}
}
}
?>
If you use your function like this you should return your html-data instead of outputting it:
public function GetTopic() {
$Connect = new mysqli("127.0.0.1", "root", "", "Data");
$Query = 'SELECT * FROM Topics';
if($Gather = $Connect->query($Query))
{
$html = "";
while($Row = $Gather->fetch_assoc())
{
$Topic = $Row['Topic'];
$html .= '<option>'.$Topic.'</option>';
}
$Gather->free();
return $html;
} else
{
//handle error
}
$Connect->close();
}
Let's try something a bit more appropriate for a class:
<?php
class Vote
{
private $connect;
public $topics = array();
public function __construct()
{
$this->connect = new mysqli( '127.0.0.1', 'root', '', 'Data' );
if( $this->connect->connect_errno )
{
echo "Error:(" . $this->connect->connect_errno . "): " . $this->connect->connect_error . ".";
}
}
public function GetTopics()
{
$Query = 'SELECT * FROM Topics';
if( $Gather = $this->connect->query( $Query ) )
{
while( $Row = $Gather->fetch_assoc() )
{
$this->topics[] = $Row['Topic'];
}
$Gather->free();
}
else
{
echo 'Error';
}
}
public function LoadTopics()
{
if( $max = count($this->topics) > 0 )
{
$html = "<select>\r\n";
for( $i = 0; $i < $max; ++$i )
{
$html .= "<option value=" . $i . ">" . $this->topics[$i] . "</option>";
}
$html .= "</select>\r\n";
return $html;
}
else
{
return false;
}
}
public function __destruct()
{
$this->connect->close();
}
}
?>
The __construct() / __destruct() methods are practically made to put your connection in. You could have also combined both functions and just have the GetTopics() method (I compulsively changed some method and property names) run the query, format the results and return the $html.
Also, I upgraded your for function, in case you decide to add another entry later to your topics, it will expand with it instead of it counting through 15 static rows.
You can call it with:
<?php
$vote = new Vote();
echo $vote->GetTopics()->LoadTopics();
?>
I saw the answer was already selected, didn't want my work to go to waste though ;D
Alternate GetTopics() function, all rolled into one.
public function GetTopics()
{
$Query = 'SELECT * FROM Topics';
if( $Gather = $this->connect->query( $Query ) )
{
$html = "<select>\r\n";
$i = 0;
while( $Row = $Gather->fetch_assoc() )
{
$html .= "<option value=" . $i . ">" . $Row['Topic'] . "</option>";
++i;
}
$html .= "</select>\r\n";
$Gather->free();
return $html;
}
else
{
return "Error: No Results Returned";
}
}
And now it's just called by:
<?php echo $vote->GetTopics(); ?>

How to store an Array to MySQL from PHP with minimum execution time

I have to store into MySQL from PHP quite a lot; usually the content to be stored I have in arrays. So I have written a small set of helper functions that saved me a lot of time.
This is the main function store that stores $array into MySQL $table:
function store($array, $table, $limit = 1000, $mode = '') {
if (empty($array)) return FALSE;
$sql_insert = "INSERT INTO `".$table."` (";
$array_keys = array_keys($array);
$keys = array_keys($array[$array_keys[0]]);
foreach ($keys as $key) $sql_insert .= "`".$key."`,";
$sql_insert = replaceEND($sql_insert, ') VALUES ', 1);
$count = 1;
$sql = $sql_insert;
foreach ($array as $array_num => $row) {
$sql .= '(';
foreach ($row as $key => $value) {
$string = $value;
if (!is_null($value)) {
$sql .= "'".str_replace("'", "\'", $string)."',";
} else {
$sql .= "NULL,";
}
}
$sql = replaceEND($sql, '),', 1);
$count = checkInsert($sql, $count, $limit);
if ($count == 0) {
if ($mode == 'PRINT') echo $sql.'<br />';
$sql = $sql_insert;
}
$count++;
}
$last = lastInsert($sql);
if ($last) {
if ($mode == 'PRINT') echo $sql.'<br />';
}
return TRUE;
}
I am using the following helpers within this function -
To add the correct ending to the statement strings:
function replaceEnd($string, $replacer = ';', $number_of_chars = 1) {
return left($string, strlen($string) - $number_of_chars) . $replacer;
}
To check, if $limit is reached which will lead to execution of the statement:
function checkInsert($sql, $sqlcount, $sql_limit) {
if ($sqlcount >= $sql_limit) {
$sql = replaceEND($sql);
query($sql);
$sqlcount = 0;
}
return $sqlcount;
}
To check for the last insert after the loop is finished:
function lastInsert($sql, $sql_insert = '') {
if (right($sql, 1) != ';') {
if ($sql != $sql_insert) {
$sql = replaceEND($sql);
query($sql);
return TRUE;
}
}
return FALSE;
}
Finally, to execute my statements:
function query($sql) {
$result = mysql_query($sql);
if ($result === FALSE) {
## Error Logging
}
return $result;
}
Basically, there is nothing wrong with these functions. But there is one problem, that I just could not solve: As you see, $limit is set to 1000 by default. This is usually perfectly fine, but when it comes to insert statements with much content / many characters per line of the insert statement, I have to reduce $limit manually in order to avoid errors. My question: does anyone know a way to automatically check if the current insert statement has reached the maximum in order to be executed correctly without wasting time by executing it too early? This would solve two problems at once: 1. I could get rid of $limit and 2. It would minimize the execution time needed to store the array into MySQL. I have just not found any approach to check the maximum possible length of a string that represents a MySQL insert statement, that I could apply before executing the statement...!?

How to display MySQL select statements in PHP / HTML

The MySQL selects are not displaying properly in the PHP / HTML
This is my code:
<?php
session_start();
require_once("database.php");
require_once("MySQL_connection.php");
/* Database connection */
$db = new MySQLConnection($config['sql_host'], $config['sql_username'], $config['sql_password'], $config['sql_database']);
$db->Connect();
unset($config['sql_password']);
/* Cron */
require_once("cron.php");
/* Display Advert */
$ad_link = $db->Query("SELECT `site_url` FROM `adverts` WHERE `zone`=1 AND `days`>0 ORDER BY RAND() LIMIT 0,1;");
$img_link = $db->Query("SELECT `image_url` FROM `adverts` WHERE `zone`=1 AND `days`>0 ORDER BY RAND() LIMIT 0,1;");
?>
<!DOCTYPE html>
<html>
<body>
<img src="<? echo $img_link ?>">
</body>
</html>
For some reason that is displaying as:
<html><head></head><body>
<img src="Resource id #7">
</body></html>
Is anybody know what is wrong?
Forgot to add the code for MYSQL_connection.php, the following code is everything within that file that is used to connect to the DB.
<?php
class MySQLConnection {
private $sqlHost;
private $sqlUser;
private $sqlPassword;
private $sqlDatabase;
private $mySqlLinkIdentifier = FALSE;
public $QueryFetchArrayTemp = array();
private $numQueries = 0;
public $UsedTime = 0;
public function __construct($sqlHost, $sqlUser, $sqlPassword, $sqlDatabase = FALSE) {
$this->sqlHost = $sqlHost;
$this->sqlUser = $sqlUser;
$this->sqlPassword = $sqlPassword;
$this->sqlDatabase = $sqlDatabase;
}
public function __destruct() {
$this->Close();
}
public function Connect() {
if($this->mySqlLinkIdentifier !== FALSE) {
return $this->mySqlLinkIdentifier;
}
$this->mySqlLinkIdentifier = mysql_connect($this->sqlHost, $this->sqlUser, $this->sqlPassword, TRUE); // Open new link on every call
if($this->mySqlLinkIdentifier === FALSE) {
return FALSE;
}
if($this->sqlDatabase !== FALSE) {
mysql_select_db($this->sqlDatabase, $this->mySqlLinkIdentifier);
}
return $this->mySqlLinkIdentifier;
}
public function Close() {
if($this->mySqlLinkIdentifier !== FALSE) {
mysql_close($this->mySqlLinkIdentifier);
$this->mySqlLinkIdentifier = FALSE;
}
}
public function GetLinkIdentifier() {
return $this->mySqlLinkIdentifier;
}
public function Query($query) {
$start = microtime(true);
$result = mysql_query($query, $this->GetLinkIdentifier());
$this->UsedTime += microtime(true) - $start;
$this->numQueries++;
if( $result === false ){
die($this->GetErrorMessage());
}
return $result;
}
public function FreeResult($result) {
mysql_free_result($result);
}
public function FetchArray($result) {
return mysql_fetch_array($result, MYSQL_ASSOC);
}
public function FetchArrayAll($result){
$retval = array();
if($this->GetNumRows($result)) {
while($row = $this->FetchArray($result)) {
$retval[] = $row;
}
}
return $retval;
}
public function GetNumRows($result) {
return mysql_num_rows($result);
}
public function GetNumAffectedRows() {
return mysql_affected_rows($this->mySqlLinkIdentifier);
}
// Helper methods
public function QueryFetchArrayAll($query) {
$result = $this->Query($query);
if($result === FALSE) {
return FALSE;
}
$retval = $this->FetchArrayAll($result);
$this->FreeResult($result);
return $retval;
}
public function QueryFirstRow($query) {
$result = $this->Query($query);
if($result === FALSE) {
return FALSE;
}
$retval = FALSE;
$row = $this->FetchArray($result);
if($row !== FALSE) {
$retval = $row;
}
$this->FreeResult($result);
return $retval;
}
public function QueryFirstValue($query) {
$row = $this->QueryFirstRow($query);
if($row === FALSE) {
return FALSE;
}
return $row[0];
}
public function GetErrorMessage() {
return "SQL Error: ".mysql_error().": ";
}
public function EscapeString($string) {
if (is_array($string))
{
$str = array();
foreach ($string as $key => $value)
{
$str[$key] = $this->EscapeString($value);
}
return $str;
}
return get_magic_quotes_gpc() ? $string : mysql_real_escape_string($string, $this->mySqlLinkIdentifier);
}
function GetNumberOfQueries() {
return $this->numQueries;
}
public function BeginTransaction() {
$this->Query("SET AUTOCOMMIT=0");
$this->Query("BEGIN");
}
public function CommitTransaction() {
$this->Query("COMMIT");
$this->Query("SET AUTOCOMMIT=1");
}
public function RollbackTransaction() {
$this->Query("ROLLBACK");
$this->Query("SET AUTOCOMMIT=1");
}
public function GetFoundRows() {
return $this->QueryFirstValue("SELECT FOUND_ROWS()");
}
public function GetLastInsertId() {
return $this->QueryFirstValue("SELECT LAST_INSERT_ID()");
}
public function QueryFetchArray($query, $all = false, $useCache = true)
{
$tempKey = sha1($query . ($all === true ? 'all' : 'notAll'));
$temp = $this->QueryFetchArrayTemp[$tempKey];
if ($temp && $useCache === true)
{
return unserialize($temp);
}
else
{
$queryResult = $this->Query($query);
$result = $all === true ? $this->FetchArrayAll($queryResult) : $this->FetchArray($queryResult);
$this->QueryFetchArrayTemp[$tempKey] = serialize($result);
return $result;
}
}
}
?>
Using your custom MySQL class, this will probably work:
$result = $db->Query("SELECT `site_url`,`image_url` FROM `adverts` WHERE `zone`=1 AND `days`>0 ORDER BY RAND() LIMIT 0,1;");
while($row = $db->FetchArray($result)) {
echo '<img src="' . $row['image_url'] . '">';
}
$db->FreeResult($result);
However, as others have pointed out, the code used in your custom MySQL class is deprecated and should be updated to use newer php methods/libraries.
You are using some custom MySQL wrapper, but you probably should fetch the result from the query using mysql_result(), mysql_fetch_array() or similar.
You would do well to read up on mysqli, which is a safer way to carry out MySQL queries. You can find the excellent documentation here: http://php.net/manual/en/book.mysqli.php and there is also a very useful tutorial here which covers simple queries like yours: http://www.phphaven.com/article.php?id=65
For your specific question, I would use the following:
$query = "SELECT `site_url`,`image_url` FROM `adverts` WHERE `zone`=1 AND `days`>0 ORDER BY RAND() LIMIT 0,1;";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// I've split these lines up to make them a little more readable.
echo '<a href="';
echo $row['site_url'];
echo '">';
echo '<img src="';
echo $row['image_url'];
echo '"</img></a>';
}
}
else {
echo 'NO RESULTS';
}
I hope this helps.

Categories