0

I have this code, that picks article content from my word press db then outputs it onto a table, using a loop. once a user select the articles they like they submit the selection to my next page call it displaypage to display the selected articles.

the selection page code looks like this.

        <?php /*
    Template Name: News Selector Template
    */
      //Pull the data
    $posts = $wpdb->get_results
    ("
    SELECT
        p1.*,
        wm2.meta_value 
    FROM
        mp_posts p1 
    LEFT JOIN
        mp_postmeta wm1
        ON (
            wm1.post_id = p1.id
            AND wm1.meta_value IS NOT NULL
            AND wm1.meta_key = '_thumbnail_id'
        )
    LEFT JOIN
        mp_postmeta wm2
        ON (
            wm1.meta_value = wm2.post_id
            AND wm2.meta_key = '_wp_attached_file'
            AND wm2.meta_value IS NOT NULL
        )
    WHERE
        p1.post_status='publish'
        AND p1.post_type='post'
    ORDER BY
        p1.post_date DESC LIMIT 0,30
    ");

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>

    <div class="container">
      <div class="header"><a href="mpasho.co.ke"><img src="/sample/images/logo_main_300x100px.png" alt="logo" name="sample_logo" width="300" height="100" id="logo" style="background-color: #FFF; display:block;" /></a> 
        <!-- end .header --></div>
      <div class="content">
        <h1>sample Top 30 Articles</h1>
        <p>Select the articles you want to add to the newsletter</p>
         <form action="http://sample.net/brands/sample/newsletter/"  method="post">
        <table width="960" border="2" summary="A collection of all the articles">
          <caption>
          Article Selection
          </caption>
        <tr>
        <th scope="col">Select</th>
        <th scope="col">Title</th>
        <th scope="col">Description</th>
        <th scope="col">Cover</th>
        <th scope="col">link</th>
          </tr>
          <?php
          // if(isset($_POST['submit'])){
           if (have_posts($posts)) {
          // var_dump($posts); die();
          foreach($posts as $post) {     
           //foreach($_POST['article_list'] as $post){       
            ?>
            <tr>
              <th scope="row"><input type="checkbox" name="article[]" value="<?php echo $post->ID; ?>" /></th>
              <td headers="article[]"><?php echo $post->post_title; ?></td>
              <td headers="article[]"><?php echo $post->post_excerpt; ?></td>
              <td headers="article[]"><?php echo '<img src="https://sample/wp-content/uploads/'.$post->meta_value.'" width="100px" alt="" />'?></td>
              <td headers="article[]"><?php echo $post->guid; ?></td>
            </tr>
          <?php 
          }
         }

          ?>
        <p><input type="hidden" name="metas" value="<?php echo implode(",", $post); ?>"></p>
        </table>
       <div>
        <p><input type="submit" name="Generate"  value="submit"/></p>

        </div>
        </form>
        <!---->
        </div>
       <!-- end .content --></div>
      <div class="footer">
        <p>sample Management System</p>
        <!-- end .footer --></div>
      <!-- end .container --></div>
    </body>
    </html>

Iv used implode to collect the array variables of all selected articles via a check box option.

on my display page i wrote this simple code to see the output of the array.

        <?php /*
    Template Name: Newsletter Template
    */

    $posts = explode(",", $_POST['metas']);

    ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    </head>

    <body>
    <?php echo $posts[0]->guid ?>
    <?php echo $posts[0]->meta_value ?>
    <?php echo $posts[0]->post_title ?>
    <?php echo $posts[0]->post_excerpt ?>
    </body>
    </html>

Now when i submit the selected articles from the first page i'm supposed to be able to access the various array elements in my display page, but that's not the case. i get the ERROR

Warning: implode(): Invalid arguments passed in /home/sampled/public_html/brands/sample/wp-content/themes/newsample/page-newsselector.php on line 164

i don't know where i went wrong with my implementation.

HELP PLEASE

  • 1
    You're imploding $post after the last $posts entry loop, so only one $post value will be imploded into the form. – Mark Baker Oct 21 '14 at 08:07
  • What you want to store in `metas` input field? – jogesh_pi Oct 21 '14 at 08:11
  • @Mark Barker then why do i get the warning, shouldnt i atleast see some data? and jogesh_pi What i want to store are the values for-> guid ,meta_value,post_title,post_excerpt – ninjanews Oct 21 '14 at 08:24
  • Because $post isn't an array, it's an object, and implode() doesn't work on objects, only on arrays. That's why you get an error – Mark Baker Oct 21 '14 at 08:25
  • @MarkBaker any suggestions on how i can accomplish this with my current code? – ninjanews Oct 21 '14 at 09:02
  • Depends exactly what you're trying to achieve, but serializing might be a better choice given that you're working with an object/array of objects – Mark Baker Oct 21 '14 at 09:08
  • What i want to store are the values for-> guid ,meta_value,post_title,post_excerpt and pass them on to my display page using the implode explode method – ninjanews Oct 21 '14 at 09:49
  • If you absolutely positively must use implode/explode (why?) rather than serialize, then you'll need to convert your array of objects to a an array of arrays - http://stackoverflow.com/questions/2476876/how-do-i-convert-an-object-to-an-array – Mark Baker Oct 21 '14 at 14:55

0 Answers0