Page 1 of 1

MYSQL FETCH, FOR EACH, SO CLOSE! PLEASE HELP!

Posted: Fri Mar 19, 2010 1:42 am
by scarface222
SEE BOTTOM POST, I JUST NEED TO FIGURE OUT FOR EACH WITH MYSQL QUERY



I tried this but it doesn't work

Code: Select all

 $link = connect(HOST, USER, PASSWORD);
$res = getContent($link, 500, 69);
      if(!$_GET['time'])
        $_GET['time'] = 0;
        $aTemp = null;
        while($row = mysql_fetch_array($res)){
      //not sure what to do...they use a list format, I am not sure what javascript will recognize
     
        }

Code: Select all

 
 case 'view':
  $data = array();
      $arr = file('messages.txt');
      if(!$_GET['time'])
        $_GET['time'] = 0;
      foreach($arr as $row) {
        $aTemp = null;
        list($aTemp['time'], $aTemp['nickname'], $aTemp['message']) = explode('|', $row); 
        if($aTemp['message'] AND $aTemp['time'] > $_GET['time'])
          $data[] = $aTemp;
      }
      //file_put_contents('debug.txt', print_r($data, true));
     break;

Code: Select all

function refresh() {
          $.getJSON(files+"daddy-shoutbox.php?action=view&time="+lastTime, function(json) {
            if(json.length) {
              for(i=0; i < json.length; i++) {
                $('#daddy-shoutbox-list').append(prepare(json[i]));
                $('#list-' + count).fadeIn('slow');
              }
              var j = i-1;
              lastTime = json[j].time;
            }
            //alert(lastTime);
          });
          timeoutID = setTimeout(refresh, 3000);
        }
        
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
            var options = { 
              dataType:       'json',
              beforeSubmit:   validate,
              success:        success
            };

Re: please put me out my misery with this problem lol...

Posted: Fri Mar 19, 2010 2:07 am
by cpetercarter
Before we go any further, the Javascript uses the jquery library. You do have jquery on your site, don't you? It will typically be called by a <script></script> bit in the head section of the web page.

The refresh() function expects the script daddy-shoutbox.php to return a JSON object. You will need to have the functions json_encode and json_decode available in php. They are available by default from (I think) php 5.2.1. A google search for "json_encode php4" will throw up some libraries which simulate json_encode for earlier php versions.

I hope this is helpful.

Re: please put me out my misery with this problem lol...

Posted: Fri Mar 19, 2010 3:39 am
by Luke
If you want people to help you, you need to give your thread a better title. When people are browsing the forums, all they see is your thread title. How is somebody supposed to know if they can help you when all they have to go on is that you have a problem. I am not coming down on you, I just want you to know how to get help on these forums... specific (yet concise) thread titles are how you get help. So... update your title please.

Re: convert php variable to json acceptable

Posted: Fri Mar 19, 2010 11:22 am
by scarface222
1) sorry luke haha I will stop naming things stupid names, I was getting pretty frustrated last night/early morning, and wasn't thinking very straight.

Thanks a lot peter carter, that is some headway, of course I included jquery first off lol, but is json expecting an array? for example, say you have $row['user'], $row['message'], $_GET['time'], how do you make it match the list/explode/array format it gave? I'm sorry I am not quite getting it, but I am not really used to working with json, and still not sure why I need it for this.

2) I have a comment system using Jquery $post/ajax function which works just fine, but I simply want to fade comments in individually to enhance the user experience, and this dumb script does that. Is JSON necessary or could I just modify my post function?

Re: convert php variable to json acceptable

Posted: Fri Mar 19, 2010 1:14 pm
by scarface222
k I tried this converted from the first post's code with no luck, I feel like I am getting closer though lol. When I echo the result in a test file I get just {"nickname":"fdfdf","message":"dfdf","time":"12"}, when I set the $_GET['time'] to 12, which is ok, but this does not even print all the results, and for some reason the javascript does not print anything when tested through the get json function.

Code: Select all

  case 'view':
  $link = connect(HOST, USER, PASSWORD);
$res = getContent($link, 500, 69);
      if(!$_GET['time'])
        $_GET['time'] = 12;
        while($row = mysql_fetch_array($res)){
$data = array(
    "nickname" => $row['user'],
    "message" => $row['message'],
     "time" => $_GET['time']
);
echo json_encode($data);     
        }
     break;

Re: convert php variable to json solved

Posted: Fri Mar 19, 2010 5:02 pm
by scarface222
see below

Re: convert php variable to json acceptable, almost there...

Posted: Sun Mar 21, 2010 2:01 am
by scarface222
k nvm it doesnt work lol, it refreshes the content 3-4 times before stopping, which makes me think it is because there is no for each statement, but I am not quite sure how to integrate one since there is already a while statement. Any ideas anyone lol?

this is mine

Code: Select all

case 'view':
  $link = connect(HOST, USER, PASSWORD);
$res = getContent($link, 500, 73);
if (!$_GET['time'])
$_GET['time'] = 0 ;
while ($row = mysql_fetch_array($res)) { 
  $aTemp = null;
        list($aTemp['time'], $aTemp['user'], $aTemp['message']) = $row; 
        if($aTemp['message'] AND $aTemp['time'] > $_GET['time'])
          $data[] = $aTemp;
}
 
     break
 
  require_once('JSON.php');
  $json = new Services_JSON();
  $out = $json->encode($data);
  print $out;;
 
This is their code

Code: Select all

 case 'view':
      $data = array();
      $arr = file('messages.txt');
      if(!$_GET['time'])
        $_GET['time'] = 0;
      foreach($arr as $row) {
        $aTemp = null;
        list($aTemp['time'], $aTemp['nickname'], $aTemp['message']) = explode('|', $row); 
        if($aTemp['message'] AND $aTemp['time'] > $_GET['time'])
          $data[] = $aTemp;
      }
    
    break;
  
require_once('JSON.php');
  $json = new Services_JSON();
  $out = $json->encode($data);
  print $out;