User
Pass
2FA
 
 

varianta afisare rezultate json

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Freakz Forum Index -> Trash Bin -> Trash -> Programming / Scripting / Database
Author Message1269
Tr3z3gu3T

[Mentally Stable]



Status: Offline
(since 12-06-2019 13:16)
Joined: 29 Sep 2006
Posts: 493, Topics: 71
Location: Romania

Reputation: 481.6
Votes: 14

 
Post Posted: 23-09-2011, 05:48:15 | Translate post to: ... (Click for more languages)

js
Code:
$(window).bind('hashchange', function(){
      url = window.location.hash.substring(1);
      if (url == '') { request = ''; } else { req = url.split('/'); request = req[1]; }
      $.getJSON('_inc/phps/json.php?'+request, function(data) {
      var htmlString = "";
      $.each(data.items, function (i, item) {
         htmlString += '<li><a href="'+item.id+'" onclick="location.hash = this.pathname; return false;">';
         htmlString += item.nume;
         htmlString += '</a></li>';
      });
        $("#content").html('<ul>'+htmlString+'</ul>');
      });
   });
   $(window).trigger('hashchange');

php
Code:
$query = $_SERVER["QUERY_STRING"];

if (empty($query)) {
   $req = "id_parinte is Null";
   $pre = "a";
} else {
   $result = mysql_query("SELECT * FROM tree WHERE id_parinte = ".$query." ORDER BY id ASC");
   $totalRows = mysql_num_rows($result);
   if ($totalRows == 0) {    
      $req = "id = " . $query;
      $pre = "b";
   } else {
      $req = "id_parinte = " . $query;
      $pre = "c";
   }
   mysql_free_result($result);
}

$result = mysql_query("SELECT * FROM tree WHERE ".$req." ORDER BY id ASC");
if (!$result) {
   die ("Could not run query: " . mysql_error());
}
while($row = mysql_fetch_array($result)) {
   $rows[$pre][] = $row;
}
print json_encode($rows);

mysql_free_result($result);
mysql_close($link);


html
Code:
<body>
<div id="header" class="wrapper"><input type="text" id="lsearch"></div>
<div id="content" class="wrapper">replacing....</div>
<div id="footer" class="wrapper">abc</div>
</body>


pana aici totul bine ma blochez la ultima parte... cum anume trebuie scris js-ul pentru a afisa rezultate diferite in functie de $pre (a,b,c)

ceva de genul if data = a { ... } care e cheita ?

0 0
  
Back to top
View user's profile Send private message Yahoo! Messenger ID
nr913
[Banned user]


Banned


Status: Offline
(since 24-10-2013 10:06)
Joined: 24 Apr 2009
Posts: 4224, Topics: 223
Location: România

Reputation: 60.3
Votes: 156

 
Post Posted: 23-09-2011, 16:29:22 | Translate post to: ... (Click for more languages)

Tu dai json_encode la $rows, și $rows ar trebui sã arate cam așa, pentru primul caz, în care $pre este 'a':
Code:
Array
(
    [a] => Array
        (
            [0] => row from tree table
            [1] => row from tree table
            [2] => row from tree table
        )
)

În cazul de fațã, parametrul data de la callback-ul getJSON-ului va fi așa:
Code:
data = {a: [{foo: 'bar'}, {foo: 'bar'}, {foo: 'bar'}]};

Unde {foo: 'bar'} este "row from tree table".
Cum vezi dacã $pre este a, b sau c? Uite așa:
Code:
if (data.a != null) alert("$pre == 'a'");
if (data.b != null) alert("$pre == 'b'");
if (data.c != null) alert("$pre == 'c'");

Și desigur, codul ãsta trebuie sã fie în callback-ul getJSON-ului.


Attitude - A little thing that makes a BIG difference
0 0
  
Back to top
View user's profile Send private message
Tr3z3gu3T

[Mentally Stable]



Status: Offline
(since 12-06-2019 13:16)
Joined: 29 Sep 2006
Posts: 493, Topics: 71
Location: Romania

Reputation: 481.6
Votes: 14

 
Post Posted: 23-09-2011, 20:47:44 | Translate post to: ... (Click for more languages)

nu inteleg "callback-ul getJSON-ului"

dar array-ul din scriptul meu e construit fix asa -

deci in final ar veni
Code:
if (data.a != null) {
   $.each(data.a, function (i, item) { 'FAC CE VREAU CU FIECARE' });
}
if (data.b != null) {
   $.each(data.b, function (i, item) { 'FAC CE VREAU CU FIECARE' });
}
if (data.c != null) {
   $.each(data.c, function (i, item) { 'FAC CE VREAU CU FIECARE' });
}


mersi frumos

0 0
  
Back to top
View user's profile Send private message Yahoo! Messenger ID
nr913
[Banned user]


Banned


Status: Offline
(since 24-10-2013 10:06)
Joined: 24 Apr 2009
Posts: 4224, Topics: 223
Location: România

Reputation: 60.3
Votes: 156

 
Post Posted: 24-09-2011, 11:23:35 | Translate post to: ... (Click for more languages)

Exact.
Code:

      $.getJSON('_inc/phps/json.php?'+request, function(data) {
         // asta e callback-ul getJSON-ului, care are ca parametru variabila 'data'
      });


Attitude - A little thing that makes a BIG difference
0 0
  
Back to top
View user's profile Send private message
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Freakz Forum Index -> Trash Bin -> Trash -> Programming / Scripting / Database  


The time now is 29-03-2024, 17:30:05
Copyright info

Based on phpBB ro/com
B

 
 
 







I forgot my password


This message appears only once, so
like us now until it's too late ! :D
x