• Hi,

    I want to display some data “in a table already created in my website” from my database.

    I have already created a part of code to filled my table with the data already stored in my database as follows:

    // creation of my table
    	<div class="my_box3">
    
                	<div class="box_title"><?php echo __("Title"); ?></div>
                    <div class="box_content">
    				<table border="1">
    				<tr><th>A1</th><th>City</th><th>A2</th><th>A3</th><th>A4</th></tr>
    				<?php 
    
    // fill my table with recovered data
    $ci=get_post_meta(get_the_ID(), 'CITY');
    $ci=array_filter($ci[0]);
    $ci=array_values($ci);
    for($i=0;$i<count($ci);$i++)
    {?>
    <tr><td><?php echo load_by_id($co[$i],"table","ID",$wpdb)->NAME;?>
    					<?php }?>
    					</table>
    					</div>
    					</div>

    It’s running locally on my PC even if I leave some empty box but when I put In in my cPanel I get this error message:

    Warning: array_filter() expects parameter 1 to be array, string given in /home/…

    Warning: array_values() expects parameter 1 to be array, null given in /home/…

    in these lines:

    $ci=array_filter($ci[0]);
    $ci=array_values($ci);

    Any help will be appreciated and Thanks in advance

Viewing 4 replies - 1 through 4 (of 4 total)
  • It may be that your site is using a later version of PHP or a more-strict error-reporting level than your local machine.

    What about this:

    / creation of my table
    	<div class="my_box3">
    
                	<div class="box_title"><?php echo __("Title"); ?></div>
                    <div class="box_content">
    				<table border="1">
    				<tr><th>A1</th><th>City</th><th>A2</th><th>A3</th><th>A4</th></tr>
    				<?php 
    
    // fill my table with recovered data
    $ci=get_post_meta(get_the_ID(), 'CITY');
    
    if (is_array($ci)) {
    	$ci = array_filter($ci, , ARRAY_FILTER_USE_KEY);
    	for($i=0;$i<count($ci);$i++) {
    		?>
    			<tr><td><?php echo load_by_id($ci[$i],"table","ID",$wpdb)->NAME;?>
    		<?php
    	}
    }
    ?>
    		</table>
    	</div>
    </div>

    It’s using a test to make sure you’ve got an array first, which should eliminate the first warning, and then sets array_values to use the key, which I think you intended.

    Thread Starter yousrakasmi

    (@yousrakasmi)

    Thank you linux4me2,

    I think the problem is with the loop for($i=0;$i<count($ci);$i++) because I have 5 columns in my table

    There is the full code to filled my table with the data

    <table border="1">
    <tr><th>Country</th><th>City</th><th>University</th><th>Department</th><th>Year</th></tr>
    <?php
    $co=get_post_meta(get_the_ID(), 'COUNTRY');
    $co=array_filter($co[0]);
    $ci=get_post_meta(get_the_ID(), 'CITY');
    $ci=array_filter($ci[0]);
    $un=get_post_meta(get_the_ID(), 'UNIVERSITY');
    $un=array_filter($un[0]);
    $de=get_post_meta(get_the_ID(), 'DEPARTMENT');
    $de=array_filter($de[0]);
    $ye=get_post_meta(get_the_ID(), 'YEARU');
    $ye=array_filter($ye[0]);
    $ci=array_values($ci);
    $co=array_values($co);
    $un=array_values($un);
    $de=array_values($de);
    $ye=array_values($ye);
    for($i=0;$i<count($co);$i++)
    {?>
    <tr><td><?php echo load_by_id($co[$i],"country","country_id",$wpdb)->COUNTRY_NAME;?></td><td><?php echo
    load_by_id($ci[$i],"city","city_id",$wpdb)->CITY_NAME?></td><td><?php echo load_by_id($un[$i],"university_attributes","pk_i_id",$wpdb)->s_name?></td><td><?php echo load_by_id($de[$i],"department_attributes","pk_i_id",$wpdb)->s_name?></td><td><?php echo $ye[$i];?></td></tr>
    <?php }?>
    </table>

    Any help will be appreciated, many thanks

    I guess I’m a bit confused about what you’re trying to accomplish. Are you just trying to get the values for country, city, university, department, and year for one post and display them in a table, or for all posts with that meta data?

    If you’re just trying to get them for one post, can’t you use something like this:

    <table border="1">
    	<tr><th>Country</th><th>City</th><th>University</th><th>Department</th><th>Year</th></tr>
    	<?php
    		$ID = get_the_ID();
    		$fields = array('co'=>'COUNTRY', 'ci'=>'CITY', 'un'=>'UNIVERSITY', 'de'=>'DEPARTMENT', 'ye'=>'YEARU')
    		foreach ($fields as $key=>$val) {
    			$$key = get_post_meta($ID, $val, true);
    		}
    	?>
    	<tr><td><?php echo $co; ?></td><td><?php echo $ci; ?></td><td><?php echo $un; ?></td><td><?php echo $de; ?></td><td><?php echo $ye; ?></td></tr>
    </table>

    Thread Starter yousrakasmi

    (@yousrakasmi)

    I’m trying to get the values for one post but each post can have a multiple values of country, city, university, department, and year and when I leave some of them empty I get the error message

    Warning: array_filter() expects parameter 1 to be array, string given
    Warning: array_values() expects parameter 1 to be array, null given

    in these lines (the empty column):
    $line=array_filter($line[0]);
    $line=array_values($line);

    So I think that the problem is with the for loop because I’ve just declared one variable in parameters for($i=0;$i<count($co);$i++)

    Can I replace this loop with another command to fill my table!

    Many thanks linux4me2

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Error during displaying results in a table from the database’ is closed to new replies.