• Resolved javaweasel

    (@javaweasel)


    I am trying to display a list of eleven items. I’ve set it as a widget in the sidebar and I’ve also tried a shortcode in a text widget box.

    There is only a single item appearing in the list, even though there are eleven items in the randomize table. I’ve checked the query that gets called, and it comes back as:

    SELECT randomize_id, text FROM randomize WHERE visible=’yes’ AND category = ‘the_list’ ORDER BY RAND() LIMIT 1

    So, in an attempt to see if the “LIMIT 1” was the issue, I removed the limit phrase in randomize.php…but that did not help. It still displays only one item.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter javaweasel

    (@javaweasel)

    …and this in the error log:
    PHP Notice: Undefined variable: result in ***/wp-content/plugins/randomize/randomize.php on line 155

    It’s in the randomize_get_category_options function. I fixed the problem, but it still only returns a single item.

    • This reply was modified 4 years, 5 months ago by javaweasel.
    Plugin Author Javik

    (@sirjavik)

    Hi there,

    sounds like a problem with the installed php version. Which version do you use?

    Thread Starter javaweasel

    (@javaweasel)

    The server is running ver. 7.3.

    Thread Starter javaweasel

    (@javaweasel)

    More info: I opened up a MySQL CLI and ran the

    SELECT randomize_id, text FROM randomize WHERE visible=’yes’ AND category = ‘the_list’ ORDER BY RAND()

    statement, and it returned all the expected items.

    It looks like it is failing in the PHP side of things.

    Thread Starter javaweasel

    (@javaweasel)

    OK…I had a major and embarrassing realization. This plugin is *supposed* to return a single, random item from a collection. In that case, it is working perfectly fine as-is.

    So, to return an entire list of randomized items as we needed, I had to rewrite the code to work for our case.

    For reference, here’s what I changed to make it work in our case:

    The result set in get_randomize returns only a single row because $wpdb->get_row() and ‘LIMIT 1’ only returns a single row by design. I removed the ‘LIMIT 1’ from the SELECT statement and changed the $wpdb->get_row() to $wpdb->get_results() and enumerated the resulting array to create a single string of a randomized list.

    	
    function get_randomize($category='', $random=false) {
    	global $wpdb;
    	$table_name = $wpdb->prefix . 'randomize';
    	$sql = 'SELECT randomize_id, text FROM '. $table_name." WHERE visible='yes' ";
    	$sql .= ($category!='') ? " AND category = '$category'" : '' ;
    	if($random)
    		//Remove the LIMIT 1 from the SELECT statement to return an entire list
    		//$sql .= ' ORDER BY RAND() LIMIT 1 ';
    		$sql .= ' ORDER BY RAND() ';
    	else
    		//s$sql .= ' ORDER BY timestamp, randomize_id LIMIT 1 ';
    		$sql .= ' ORDER BY timestamp, randomize_id ';
    	//Remove this action, since it returns only a single row.
    	//$row = $wpdb->get_row($sql);
    	
    	//New stuff to enable returning an entire list separated with <br /> tags
    	$strResult = "";
    	$arrResult = $wpdb->get_results($sql);
    	foreach ($arrResult as $row) {
    		$strResult .= $row->text . '<br />';
    	
        		// update the timestamp of the row we just selected (used by rotator, not by random)
        		if(!$random AND intval($row->randomize_id)) {
        			$sql = 'UPDATE '.$table_name.' SET timestamp = Now() WHERE randomize_id = '.intval($row->randomize_id);
        			$wpdb->query($sql);
        		}
    	}
    		
    		// now we can safely render shortcodes without self recursion (unless there is only one item containing [randomize] shortcode - don't do that, it's just silly!)
    	$snippet = do_shortcode($strResult);
    	
    	return $snippet;
    }
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Widget and shortcode just returning single item’ is closed to new replies.