Jump to content

Skewled

Members
  • Posts

    387
  • Joined

  • Last visited

Posts posted by Skewled

  1. Yes I know what I want to achive, one blog post with the comments under each post. All of the trouble started with MySQL returning multiple rows because of the JOIN, the php array was rebuilt but it needs to filter the array more. 

     

    Thank you for all the help!!

     

    Marked Solved because the jQuery was correct to achieve the desire results with the JSON data passed to the function, the PHP array needs to be re-factored to compile the JSON data better.

  2. The extra post:

     

    (First Post of the Blog)
    Title: blog post one
    Image: public/img/blog-image.png
    Dateposted: 2014-04-13
    Content:
    blog article one

    (Comment Stays)
    Login: admin
    Username: Gaddam
    Comment: blog post one comment

    (Filter this out because only needs to be displayed once)
    Title: blog post one
    Image: public/img/blog-image.png
    Dateposted: 2014-04-13
    Content:
    blog article one

     

    (Comment Stays)
    Login: admin
    Username: Gaddam
    Comment: blog post one comment

     

     

    I'm trying to filter out the need to repost the title, image, dateposted, and content for each comment.

  3. Output:

    Blog ID: 36
    Dateposted: 2014-04-13
    Content:
    blog article one
    
    Title: blog post one
    Published: 1
    Login: admin
    Image: public/img/blog-image.png
    Username: Gaddam
    Comment: blog post one comment
    Blog ID: 36
    Dateposted: 2014-04-13
    Content:
    blog article one
    
    Title: blog post one
    Published: 1
    Login: admin
    Image: public/img/blog-image.png
    Username: Gaddam
    Comment: blog post one comment
    Blog ID: 37
    Dateposted: 2014-04-13
    Content:
    blog article two
    
    Title: blog post two
    Published: 1
    Login: admin
    Image: public/img/blog-image.png
    Username: Skewled
    Comment: blog two comment
    Blog ID: 37
    Dateposted: 2014-04-13
    Content:
    blog article two
    
    Title: blog post two
    Published: 1
    Login: admin
    Image: public/img/blog-image.png
    Username: Skewled
    Comment: blog two comment
  4. No offense taken Jacques!

     

    I was just showing how to create the session along with some cookie information, I should have certainly answered the OP question better so I am rather embarrassed  if anything lol. Use the code as an example for setting session variables and establishing cookies, and verification on pages that need to know if a session exists, don't use it verbatim.

  5. I would do it like this

    <?php
    
    if (isset($_POST['submit'])) {
     // You'd want to verify the POST data though
        echo "$newUrl = 'http://www.myothertestspace.com/simlpe2.php?name=' . $_POST['name'] . '&surname=' . $_POST['lastname'] . '&phone=' . $_POST['phone']";
    
         }
    
    ?>
    
    <html>
    <form name="Students Management"action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" accept-charset="utf-8">
    
    <p>
    <label>First Name</label>
    <input type="text" value="" name="firstname"  ></input>
    </p>
    <p>
    <label>Phone</label>
    <input type="text" value="" name="phone"  ></input>
    </p>
    <p>
    <label>Last Name</label>
    <input type="text" value="" name="lastname"  required="true"></input>
    </p>
    <p>
    <label>Company</label>
    <input type="text" value="" name="school"  required="true"></input>
    </p>
    <p>
    <label>Email</label>
    <input type="text" value="" name="email"  ></input>
    </p>
    <p>
    <label>Country</label>
    <input type="text" value="" name="age"  ></input>
    </p>
    <p>
    <input type="submit" value="Submit" ></input>
    </p>
    </form>
    </html>

    You could apply some CSS and place the output into an iframe if you wanted.

  6. I've used a session.php file that I coded that is bare minimum:

     

    <?php
    header("Cache-Control: no-cache");
      session_start();
      // If the session vars aren't set, try to set them with a cookie
      if (!isset($_SESSION['user_id'])) {
        if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) {
          $_SESSION['user_id'] = $_COOKIE['user_id'];
          $_SESSION['username'] = $_COOKIE['username'];
    } else {
    header('Location: /login.php');
        }
      }
    ?>

    Login:

     

    I would then have a form that checks to see if the data exsist for the login and do the following:

              require_once('session.php');  // Start the session
         session_start();
    
      // Query database code goes here 
         $query = " "; // SELECT SOMETHING FROM USERS WHERE USERNAME = SOMETHING AND PASSWORD = SOMETHING
         $data = mysqli_query($dbc, $query);     if (mysqli_num_rows($data) == 1) {
               // The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page
              $row = mysqli_fetch_array($data);
              $_SESSION['user_id'] = $row['id'];
              $_SESSION['username'] = $row['username'];
              setcookie('user_id', $row['id'], time() + (60 * 60 * 24 * 30));    // expires in 30 days
              setcookie('username', $row['username'], time() + (60 * 60 * 24 * 30));  // expires in 30 days
              $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php';
              header('Location: ' . $home_url);
        } else {
              // The username/password are incorrect so set an error message
              $error_msg = '<p class="error">Sorry, you must enter a valid username and password to log in.<br>';
            }
    Each page the user would access would require an active session, using the session.php file. So if they didn't have a valid cookie anymore they would need to log back in, each login resets the session data.
     
    Hope this helps you out.

     

  7. Denno,

     

     Thank you for your reply, I've been doing some reading about $(each) and it functions like a for loop. I know I need to come up with a method of storing the first blog_id and then during each iteration store the value so that on the next iteration it skips over the data but I can't figure out how to put it into code. I've been playing with the jfiddle and reading around on google, lot's of helpful information but I haven't found anything with filtering the data or iterating over the data. I blame my poor search skills lol.

     

     Currently it outputs:

     Blog Post Two
    
     Blog Post Two Comment
    
     Blog Post Two
    
     Blog Post Two Comment

    Here is the jsfiddle: http://jsfiddle.net/66c8z/1/, in this it actually shows the data somewhat correctly but just has the duplicates.

     

    Updated Function:

        var load_blog = function() {
            $.getJSON('api/get_blog', function(data) {
    
    
               $.each(data, function() { //Loop through each blog_id section
                    var output = '';
                        $.each(this, function(){ //Loop through each comment in this blog_id
                            output += Template.blog(this); //output the template
                        }); 
                    $("#list_blog").html(output);
                });
            });
            
        };

     

     

     

  8. I have the following JSON string of data that I'm trying to loop over:

     

    {
      "35": [
        {
          "blog_id": "35",
          "dateposted": "2014-04-10",
          "content": "<p>Wow this is <span style=\"color: #008000;\"><em><span style=\"font-size: 14pt;\"><strong>very nice...<\/strong><\/span><\/em><\/span><\/p>",
          "title": "Thanks",
          "published": "1",
          "login": "admin",
          "img": "public\/img\/blog-image.png",
          "username": "Skewled",
          "comment": "thanks"
        },
        {
          "blog_id": "35",
          "dateposted": "2014-04-10",
          "content": "<p>Wow this is <span style=\"color: #008000;\"><em><span style=\"font-size: 14pt;\"><strong>very nice...<\/strong><\/span><\/em><\/span><\/p>",
          "title": "Thanks",
          "published": "1",
          "login": "admin",
          "img": "public\/img\/blog-image.png",
          "username": "Skewled",
          "comment": "fasdfa asdf asd fasdfasd [b]test[\/b]"
        }
      ],
      "34": [
        {
          "blog_id": "34",
          "dateposted": "2014-04-09",
          "content": "<p><em><strong>sdfasdf asdafsadf<\/strong> asdfasd <\/em> asdfasdf <span style=\"color: #008000;\">asdf asdf asdf asdfsa asfasdfaasdf <span style=\"color: #000000;\">asdfasdf<\/span><\/span><\/p>\n<p><span style=\"color: #008000;\"><span style=\"color: #000000;\">saf asdfasd <strong>fasdfasdf asdfasdf<\/strong> asdf adsfa asdfasdf asdf asdf asdfasdfa sdf asdf<\/span><\/span><\/p>\n<p><span style=\"color: #008000;\"><span style=\"color: #000000;\">sadfs dfasdf asdf asdf asdf<\/span><\/span><\/p>",
          "title": "Launch Party",
          "published": "1",
          "login": "admin",
          "img": "public\/img\/blog-image.png",
          "username": "Skewled",
          "comment": "<p><em><strong>sdfasdf asdafsadf<\/strong> asdfasd <\/em> asdfasdf <span style=\"color: #008000;\">asdf asdf asdf asdfsa asfasdfaasdf <span style=\"color: #000000;\">asdfasdf<\/span><\/span><\/p>\n<p><span style=\"color: #008000;\"><span style=\"color: #000000;\">saf asdfasd <strong>fasdfasdf asdfasdf<\/strong> asdf adsfa asdfasdf asdf asdf asdfasdfa sdf asdf<\/span><\/span><\/p>\n<p><span style=\"color: #008000;\"><span style=\"color: #000000;\">sadfs dfasdf asdf asdf asdf<\/span><\/span><\/p>"
        },
        {
          "blog_id": "34",
          "dateposted": "2014-04-09",
          "content": "<p><em><strong>sdfasdf asdafsadf<\/strong> asdfasd <\/em> asdfasdf <span style=\"color: #008000;\">asdf asdf asdf asdfsa asfasdfaasdf <span style=\"color: #000000;\">asdfasdf<\/span><\/span><\/p>\n<p><span style=\"color: #008000;\"><span style=\"color: #000000;\">saf asdfasd <strong>fasdfasdf asdfasdf<\/strong> asdf adsfa asdfasdf asdf asdf asdfasdfa sdf asdf<\/span><\/span><\/p>\n<p><span style=\"color: #008000;\"><span style=\"color: #000000;\">sadfs dfasdf asdf asdf asdf<\/span><\/span><\/p>",
          "title": "Launch Party",
          "published": "1",
          "login": "admin",
          "img": "public\/img\/blog-image.png",
          "username": "Skewled",
          "comment": "<p><em><strong>sdfasdf asdafsadf<\/strong> asdfasd <\/em> asdfasdf <span style=\"color: #008000;\">asdf asdf asdf asdfsa asfasdfaasdf <span style=\"color: #000000;\">asdfasdf<\/span><\/span><\/p>\n<p><span style=\"color: #008000;\"><span style=\"color: #000000;\">saf asdfasd <strong>fasdfasdf asdfasdf<\/strong> asdf adsfa asdfasdf asdf asdf asdfasdfa sdf asdf<\/span><\/span><\/p>\n<p><span style=\"color: #008000;\"><span style=\"color: #000000;\">sadfs dfasdf asdf asdf asdf<\/span><\/span><\/p>"
        }
      ]
    }

    I have the following loop but since the data change to a multidimensional setup I've gotten a bit lost, so if anyone would be willing to walk me through the process so I can learn I'd really appreciate it.

     


    The is my api call to grab the data above:

        var load_blog = function() {
            $.get('api/get_blog', function(o) {
                
                var output = '';         
                        
                for (var i = 0; i < o.length; i++) {
                    output += Template.blog(o[i]);
                } 
                
                $("#list_blog").html(output);  
            }, 'json');
    
    
        };

    I then have the HTML template separate from the code here:


        this.blog = function(obj) {
            var output = '';
            output += '<div class="blog-post" style="margin: 5px;">';
            output += '<h2 class="blog-post-title">';
            output += obj.title + '</h2>';
            output += '<p class="blog-post-meta">';
            output += '<img src="' + obj.img +'">' + ' ' + obj.dateposted + ' by ' + obj.login + '</p>';
            output += '<p>' + obj.content + '</p>';
            output += '</div>';
            output += '<span class="options">';
            output += '<a class ="blog_update_display" data-id="' + obj.blog_id + '" href="#"><i class="glyphicon glyphicon-pencil"></i></a> Leave Comment ';
            output += '<div class="blog_comment_container" id="blog_comment_container_'+ obj.blog_id +'"></div>';
            output += '<div class="hide" id="blog_comment_' + obj.blog_id + '">' + obj.comment + '</div>';
            output += '</span>';
            output += '<hr>';
            output += '<span><a class="comment_toggle" data-id="'+ obj.blog_id +'" id="blog_title_' + obj.blog_id + '" href="#">View Comments</a></span> ';
            output += '<div class="hide" id="blog_comment_block_' + obj.blog_id + '">';
            output += '<hr>';
            if (obj.username) {
            output += '<h5 class="users">' + obj.username + ' commented:</h5>';
            output += '<p>' + obj.comment + '</p>';
            } else {
                output += '<p>Be the first to comment!</p>';
            }
            output += '</div>';
            output += '<hr>';
            output += '</div>';
            return output;
        };

    I'm not sure how to change my template to work with the new object data.

     

    Thanks!

  9. Thank you guys, I just need to work with that array in jQuery as you mentioned.. it's not easy to digest information like this and I appreciate your patience and for each of you re-iterating the same solution!

  10. I would just disable the submit button until they check the box.

    $('#opt-in-checkbox').click(function(){
       if($(this).attr('checked') == false){
          $('#wpsp-continue').attr("disabled","disabled"); 
       } else {
          $('#wpsp-continue').removeAttr('disabled');
       }
    });

    Feedback: I would have an age verification such as a birthday added to the form and server side checking because you can disable javascript.

  11. 
    
    <script>
    $(document).ready(function() {
       $("a").click(function(e) {
         e.preventDefault;
          var c = confirm('If you leave your upload may fail.');
            if (c == false) return false;
       });
    });
    </script>
     

    This will alert on any link clicked on the page and if they click ok they will be able to follow the link or if they click cancel it won't do anything.

  12. I think your missing some header information for sending a file in the email.

    $header = '';
    $headers .= "From:" . $to;
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"\"\n\n";
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "Content-type: text/html; charset=utf-8\n";
    $headers .= "Content-Transfer-Encoding: 7bit\n\n";
  13. Let me just say I love this community and I appreciate all of the input I've gotten, I've learned so much from the help I've received here!

     

    Here is the plan in my mind:

     

    1. Fetch the data from the database

    2. Filter the data so that each blog only get's displayed once and all of the comments are looped underneath it.

        - I'm using jQuery for the client view and currently it's setup as a MVC as well, so functions are seperated from the view which calls from a template which is just the     HTML of the view with the variables that I want to display to the client area.

    3. Allow additions to the comments by users

       - jQuery post and update the html area for the comments to append the post

     

    I'm doing this to learn something new and I've gotten the system going except this issue, so I'm wondering if my setup is incorrect and I should redo the whole thing or I'm going in the right direction and continue learning more. Eventually I would love to paginate the results using jQuery so I can keep all the blog posts, and an Archive.

     

    I've gotten the basics of PHP down and I've moved into the OOP side which has certainly been rewarding and I continue to practice and learn. So I am openly embracing all input I receive here that can help solidify the knowledge I've gained. I'm posing the setup I've coded so everyone can see what I'm working with, I think so far I'm pretty happy with what I've come up with so far but please any input is welcomed!! (:

     

    So here is a basic example of my overall setup:

     

    PHP API Call:

     

        public function get_blog()
        {               
                $query = $this->db->query(
                    "SELECT blog.blog_id, blog.dateposted, blog.content, blog.title, blog.published,
                        user.login, user.img,
                        bc.username, bc.comment
                        FROM blog
                        JOIN user
                          ON blog.user_id = user.user_id
                        LEFT JOIN blog_comments bc
                          ON blog.blog_id = bc.blog_id
                        WHERE blog.published = 1
                        ORDER BY blog.blog_id DESC
                        ");
                
                   $data = $query->result_array();
                   
                    $outputArr = array();
    
    
                    foreach ($data as $result) {
                      //Initialise array for the blog ID of the current $result, if it's not already done so
                      if (!isset($outputArr[$result['blog_id']])) {
                        $outputArr[$result['blog_id']] = array();
                      }
                      //Append the comment_text for this result to the array for this blog_id
                      $outputArr[$result['blog_id']]['blog_id'] = $result['blog_id'];
                      $outputArr[$result['blog_id']]['dateposted'] = $result['comment'];
                      $outputArr[$result['blog_id']]['content'] = $result['content'];
                      $outputArr[$result['blog_id']]['title'] = $result['title'];
                      $outputArr[$result['blog_id']]['published'] = $result['published'];
                      $outputArr[$result['blog_id']]['login'] = $result['login'];
                      $outputArr[$result['blog_id']]['img'] = $result['img'];
                      $outputArr[$result['blog_id']]['username'] = $result['username'];
                      $outputArr[$result['blog_id']]['comment'] = $result['comment'];
                    }               
                    
                    $result = $outputArr;
    
    
            if ($result) {
               $this->output->set_output(json_encode($result));
               return false;
            }
        }
     
     

    Template for the View:

     this.blog = function(obj) {
            var output = '';
            output += '<div class="blog-post" style="margin: 5px;">';
            output += '<h2 class="blog-post-title">';
            output += obj.title + '</h2>';
            output += '<p class="blog-post-meta">';
            output += '<img src="' + obj.img +'">' + ' ' + obj.dateposted + ' by ' + obj.login + '</p>';
            output += '<p>' + obj.content + '</p>';
            output += '</div>';
            output += '<span class="options">';
            output += '<a class ="blog_update_display" data-id="' + obj.blog_id + '" href="#"><i class="glyphicon glyphicon-pencil"></i></a> Leave Comment ';
            output += '<div class="blog_comment_container" id="blog_comment_container_'+ obj.blog_id +'"></div>';
            output += '<div class="hide" id="blog_comment_' + obj.blog_id + '">' + obj.comment + '</div>';
            output += '</span>';
            output += '<hr>';
            output += '<span><a class="comment_toggle" data-id="'+ obj.blog_id +'" id="blog_title_' + obj.blog_id + '" href="#">View Comments</a></span> ';
            output += '<div class="hide" id="blog_comment_block_' + obj.blog_id + '">';
            output += '<hr>';
            if (obj.username) {
            output += '<h5 class="users">' + obj.username + ' commented:</h5>';
            output += '<p>' + obj.comment + '</p>';
            } else {
                output += '<p>Be the first to comment!</p>';
            }
            output += '</div>';
            output += '<hr>';
            output += '</div>';
            return output;
        };
    
    
        // ------------------------------------------------------------------------
        
        this.blog_comment = function(blog_id) {
            var output = '';
            output += '<form method="post" class="blog_comment_form form-horizontal" action="../api/post_comment">';
            output += '<input class="blog_id" type="hidden" name="blog_id" value="' + blog_id + '" />';
            output += '<div class="input-append">';
            output += '<label for="comment">Comment:</label><br>';
            output += '<textarea style="width:100%" rows="10" class="comment" name="comment" id="comment"></textarea>';
            output += '<input tabindex="3" type="submit" class="btn btn-success" value="Post Comment" />';
            output += '<input type="submit" class="btn blog_comment_cancel" value="Cancel" />';        
            output += '</form>';
            return output;
        };

    Events:

        var toggle_comments = function() {
          $('body').on("click", ".comment_toggle", function (evt) {
              evt.preventDefault;
              
              var blog_id = $(this).data('id');
              
              $("#blog_comment_block_" + blog_id).toggleClass('hide');
              return false;
          });
        };
        
        // ------------------------------------------------------------------------
        
        var add_comment = function() {
            $("body").on("submit", ".blog_comment_form", function(evt) {
               evt.preventDefault();
               
               var form = $(this);
               var url = $(this).attr('action');
               var postData = {
                   blog_id: $(this).find('.blog_id').val(),
                   comment: $(this).find('.comment').val()
               };
               
               //console.log(postData);          
               
               $.post(url, postData, function(o) {
                   if(o.result == 1) {
                       Result.success("Successfuly Added Comment.");                     
                       $("#blog_comment_" + postData.blog_id).html(postData.comment);
                       form.remove();
                   } else {
                       Result.error("No Changes Made.");
                  }
               }, 'json');
            });   
        };
        
        
        // ------------------------------------------------------------------------
        
        var update_blog_display = function() {
            $("body").on('click', '.blog_update_display', function(e) {
                // Debug: console.log(e);
                e.preventDefault();                     
                
                var blog_id = $(this).data('id');
                var output = Template.blog_comment(blog_id);
    
    
                $("#blog_comment_container_" + blog_id).html(output);
            });
            
            $("body").on("click", ".blog_comment_cancel", function(e) {
                e.preventDefault();       
                $(this).parents('.blog_comment_container').empty();  
            });
        }; 

    Loading Code:

     

        var load_blog = function() {
            $.get('api/get_blog', function(o) {
                
                var output = '';
                        
                for (var i = 0; i < o.length; i++) {
                    output += Template.blog(o[i]);
                } 
                
                $("#list_blog").html(output);  
            }, 'json');
        };
  14. Denno,

     

     Thank you for the reply and I see what you were doing for the most part. I have difficulty when working with data and arrays so I'm practicing.

     

    Here is the data returned by my query:

     


    Array
    (
        [0] => Array
            (
                [blog_id] => 35
                [dateposted] => 2014-04-10
                [content] => <p>Wow this is <span style="color: #008000;"><em><span style="font-size: 14pt;"><strong>very nice...</strong></span></em></span></p>
                [title] => Thanks
                [published] => 1
                [login] => admin
                [img] => public/img/blog-image.png
                [username] => Skewled
                [comment] => thanks
            )
    
    
        [1] => Array
            (
                [blog_id] => 35
                [dateposted] => 2014-04-10
                [content] => <p>Wow this is <span style="color: #008000;"><em><span style="font-size: 14pt;"><strong>very nice...</strong></span></em></span></p>
                [title] => Thanks
                [published] => 1
                [login] => admin
                [img] => public/img/blog-image.png
                [username] => Skewled
                [comment] => fasdfa asdf asd fasdfasd [b]test[/b]
            )
    
    
        [2] => Array
            (
                [blog_id] => 34
                [dateposted] => 2014-04-09
                [content] => <p><em><strong>sdfasdf asdafsadf</strong> asdfasd </em> asdfasdf <span style="color: #008000;">asdf asdf asdf asdfsa asfasdfaasdf <span style="color: #000000;">asdfasdf</span></span></p>
    <p><span style="color: #008000;"><span style="color: #000000;">saf asdfasd <strong>fasdfasdf asdfasdf</strong> asdf adsfa asdfasdf asdf asdf asdfasdfa sdf asdf</span></span></p>
    <p><span style="color: #008000;"><span style="color: #000000;">sadfs dfasdf asdf asdf asdf</span></span></p>
                [title] => Launch Party
                [published] => 1
                [login] => admin
                [img] => public/img/blog-image.png
                [username] => Skewled
                [comment] => <p><em><strong>sdfasdf asdafsadf</strong> asdfasd </em> asdfasdf <span style="color: #008000;">asdf asdf asdf asdfsa asfasdfaasdf <span style="color: #000000;">asdfasdf</span></span></p>
    <p><span style="color: #008000;"><span style="color: #000000;">saf asdfasd <strong>fasdfasdf asdfasdf</strong> asdf adsfa asdfasdf asdf asdf asdfasdfa sdf asdf</span></span></p>
    <p><span style="color: #008000;"><span style="color: #000000;">sadfs dfasdf asdf asdf asdf</span></span></p>
            )
    
    
        [3] => Array
            (
                [blog_id] => 34
                [dateposted] => 2014-04-09
                [content] => <p><em><strong>sdfasdf asdafsadf</strong> asdfasd </em> asdfasdf <span style="color: #008000;">asdf asdf asdf asdfsa asfasdfaasdf <span style="color: #000000;">asdfasdf</span></span></p>
    <p><span style="color: #008000;"><span style="color: #000000;">saf asdfasd <strong>fasdfasdf asdfasdf</strong> asdf adsfa asdfasdf asdf asdf asdfasdfa sdf asdf</span></span></p>
    <p><span style="color: #008000;"><span style="color: #000000;">sadfs dfasdf asdf asdf asdf</span></span></p>
                [title] => Launch Party
                [published] => 1
                [login] => admin
                [img] => public/img/blog-image.png
                [username] => Skewled
                [comment] => <p><em><strong>sdfasdf asdafsadf</strong> asdfasd </em> asdfasdf <span style="color: #008000;">asdf asdf asdf asdfsa asfasdfaasdf <span style="color: #000000;">asdfasdf</span></span></p>
    <p><span style="color: #008000;"><span style="color: #000000;">saf asdfasd <strong>fasdfasdf asdfasdf</strong> asdf adsfa asdfasdf asdf asdf asdfasdfa sdf asdf</span></span></p>
    <p><span style="color: #008000;"><span style="color: #000000;">sadfs dfasdf asdf asdf asdf</span></span></p>
            )
    )

    As you can see each blog_id is repeated with a comment, so I'm trying to restructure it so each blog_id is only posted once to the array and then the comments are posted into it. Would I need to add another array to this for each comment so the key's would be unique based off the comment_id?

     

    I really appreciate your help and your reply was very clear and concise, my questioning and logic are faulting me here lol.

     

    Thanks!

     

     

    EDIT:

     

    I wanted to show you the json_encoded output that I was working with:

     

    non-filtered:

    https://www.dropbox.com/s/nsq8vas9469uzit/output0.png

     

    filtered:

    https://www.dropbox.com/s/4a3h8o7649ee1fo/output1.png

     

     

  15. I'm using codeigniter as the framework and I have a mysql return that is being json_encode() so that my jQuery script can work with the data to display it in my view.
     
    I'm doing this as a learning exercise and I'm stumped. I want to take the result_array() from the query and manipulate the data, so I would like to have anyone willing to help me, to walk me through the issue so I can learn.
     
    Data from MySQL:

    blog_id |  content | comment_id | comment_text
    -------------------------------------------------
     2        Blog2Text    4           comment4text
     2        Blog2Text    7           comment7text
     2        Blog2Text    8           comment8text
     5        Blog5Text    9           comment9text
     5        Blog5Text    11          comment11text

    This works for retrieving the data perfectly and I can credit Psycho for assistance with my query from here: http://forums.phpfreaks.com/topic/287690-need-help-with-joins/

     

    So fear each blog as in blog_id / content I want to have the unique comments posted with them. So the data should now be filtered and output:

    Blog2Text
    comment4text
    comment7text
    comment8text
    
    Blog5Text
    comment8text
    comment9text
    comment11text

    In my mind I'm doing the following pseudo code:

     

    1. Create a variable to store the blog_id

    2. Set up condition to scan the array for each unique blog_id and add it to a new array along with the comments for that blog_id

    3. Return the new array json_encode() from the function

     

    Thank You!

  16. Psycho, Jazzman, and Barand

     

     Thank you each for your input, I now understand what I was doing wrong and what I needed to do for a solution. The version of MySQL I'm using won't allow the Limit subquery you created, I will have to do something for limiting or breaking the data into pages but that would be on the PHP end. The example you posted really helped me visualize what Jazzman was talking about and really gave me a huge leg up on completing a personal project.

     

     Thanks!!

  17. Thank you each for your input, I also read about GROUP_CONCAT and discovered the limit so I choose to not use it as a viable solution. 

     

    Jazzman, you mention pulling the data and then using an application language technique, I've never heard it put that way but are you referring to having PHP(or server side language) parse the data and re-organize it?

     

     I'm thinking the simplistic approach would have to be using 2 queries and tacking the comments to each blog_id?

     

    Again, thank each of you for your input and wisdom thus far.

  18. Psycho,

     

    Thank you for the very clear explanation and what I'm trying to achieve by the limit was to have 5 of the main blog posts to be pulled, and than all of the comments for those blogs to be pulled to be displayed using jQuery. The issue with the query is it's posting each blog multiple times for each comment posted, which is why I used the Group By but I clearly misunderstood the use. I will use aliases differently now as well.

     

    I placed the username in the comment table to make it easy to pull latest comments using 1 query without a JOIN.

     

    Is it possible to achieve what I need in a single query and if so where is my logic failing me?

     

    Thank You!

  19. I have the following query:

    "SELECT a.blog_id, a.dateposted, a.content, a.title, a.published, b.login,
     b.img, c.username, c.comment
            FROM blog a
            JOIN user b
                ON a.user_id = b.user_id
            RIGHT JOIN blog_comments c
                ON a.blog_id = c.blog_id
            WHERE a.published = 1
            GROUP BY a.dateposted DESC
            LIMIT 5
            "

    I have gotten the proper data but I can't figure out how to get the all of the comments for each blog post, currently it will only join the rows and add the first comment, and leave the remaining comments out.

     

    Tables are:

     

    blog

     

    blog_id - Primary | dateposted | content | title | published

     

    user

     

    user_id - Primary | login | img 

     

    blog_comments

     

    comment_id - Primary | blog_id - FKey | comment

     

     

    I appreciate the assistance, I'm new to JOINS and my Google reading has allowed me to compile this query so far but it's getting over my head lol.

  20. Let's take a look at the upload section first.

        public function upload()
        {
     
    		$submit = $this->input->post("submit");
    		
    		$bank = $this->input->post("bank");
    		$card = $this->input->post("card");
    		$amount = $this->input->post("amount");
    		
    		$this->data['post']['bank'] 	= $bank;
    		$this->data['post']['card'] 	= $card;
    		$this->data['post']['amount'] 	= $amount;
    		
    		if($submit)
    		{ 
    		
    			if(empty($bank) && empty($card))
    			{
    				 
    				 $this->data['post']['error_message'] = "<a href=\"#\" class=\"notification error\">Please select either a bank account or debit/credit card.</a>";
                    
    			}
    			elseif(!empty($bank) && !empty($card))
    			{
    			
    				$this->data['post']['error_message'] = "<a href=\"#\" class=\"notification error\">Please only choose one upload method at a time.</a>";
    				
    			}
    			elseif(empty($amount))
    			{
    				
    				 $this->data['post']['error_message'] = "<a href=\"#\" class=\"notification error\">Please enter an amount to withdraw.</a>";
                    
    			}
    			else 
    			{
    			
    				$accountType = $this->data['account']->accountType;
     
    	            $fees = $this->calculateFees('Withdrawal', $accountType, $amount);
    	            $fees = $fees->fees;
    	            $fees = ($amount * ($fees / 100));
    	
    	            $final = ($amount - $fees);
    				
    				$this->db->query("INSERT INTO upload_transactions (accountID, amount, bankID, cardID, status) VALUES ('". $this->account_id ."', '". $amount ."', '". $bank ."', '". $card ."', '1')");
    				
    				$this->db->query("UPDATE accounts SET amount = (amount + ". $final .") WHERE id = '". $this->account_id ."'");
    				
    				$this->data['post']['error_message'] = "<a href=\"#\" class=\"notification success\">Your (€". $amount ." + Fees: €". $fees .") upload was successful.</a>";
    			       
    				$this->data['post']['bank'] = '';
    				$this->data['post']['amount'] = '';
    				$this->data['post']['card'] = '';
    				
    			}
    			
    			
    		}
     
            $this->data['site']['currentNav'] = "account";
     
            $query = $this->db->query("SELECT
            							  C.id,
                                          C.card_number AS `card_number`
                                        FROM `cards` C
                                        WHERE
                                          C.accountID = '". $this->account_id ."'
                                        AND
                                          C.status = '1'");
     
            $this->data['cards'] = $query->result();
     
            $query = $this->db->query("SELECT
            							  B.id,
                                          B.account_number AS `account_number`,
                                          B.sortcode AS `sortcode`
                                        FROM `banks` B
                                        WHERE
                                          B.accountID = '". $this->account_id ."'
                                        AND
                                          B.status = '1'");
     
            $this->data['banks'] = $query->result();
     
            $this->load->view('_template/header', $this->data);
            $this->load->view('account/upload', $this->data);
            $this->load->view('_template/footer', $this->data);
        }
    

    What is not working properly?

    1. The form is submitted

    2. Form Validation Occurs

    3. Transaction is recorded to the Database

    4. Account information is updated with the transaction amount

    5. Navigation change to account page

    6. Query for Cards

    7. Query for Banks

    8. The view is then loaded for header, upload, and footer.

     

    I see that you perform:

    $this->load->view('account/upload', $this->data);

    So account/upload view has the needed code to parse that data array to display the information you want? So data['cards'] and data['banks'] would be used there.

  21. I've been using CodeIgniter for a project I'm working on and I have become quite fond of it.

     

    If you want your Overview View to display the information than you need to keep it in the Overview Controller for consistency.

     

    Does this Controller actually load the view properly? Typically the Controller is a class that links to a model and view but I guess you didn't post the entire Controller?

  22. You might want to look in the job offerings section of phpfreaks.com  and post this job to find a coder who will do this for you, of course a nominal fee would be applied.

     

    Here is a link: http://forums.phpfreaks.com/forum/77-job-offerings/

     

    - OR -

     

     You could come up with some code and if you get errors reach out along the way, but I wouldn't recommend posting every error you get without consulting google first.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.