|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Mar 2011
Messaggi: 2
|
[PHP] html e json
Ciao,
sto scrivendo degli script che leggono in un db dei valori che poi vengono rappresentati in json. Fino a qui non ci sono problemi, però in uno dei campi della tabella è presente dell'htm in particolare una table con diverse colonne e un link. Come posso leggere dentro l'html "prendere" due colonne di questa tabella, il link e rappresentarli in json (insieme al resto)? Questo è lo script: Codice:
<?php
require("../script.php");
$data="";
$query="";
$timestamp = 0;
if(isset($_GET['time'])){
$timestamp = $_GET['time'];
$data = date("Y-m-d H:i:s",($timestamp-3600));
$query ="SELECT `id`, `title`, `alias`, `catid`, `introtext`, `fulltext`, `extra_fields` FROM `h5ojb_k2_items` WHERE catid=29 and modified > "."'".$data."' ";
}else{
$query ="SELECT `id`, `title`, `alias`, `catid`, `introtext`, `fulltext`, `extra_fields` FROM `h5ojb_k2_items` WHERE catid=29";
}
$result = mysql_query($query);
$arrayProduct = Array();
$i=0;
//echo $query;
while($obj = mysql_fetch_array($result)) {
$arrayProduct[$i] = Array();
$arrayProduct[$i]["id"] = $obj['id'];
$arrayProduct[$i]["title"] = $obj['title'];
$arrayProduct[$i]["alias"] = strip_tags($obj['alias']);
$arrayProduct[$i]["introtext"] = $obj['introtext'];
$arrayProduct[$i]["fulltext"] = $obj['fulltext'];
$arrayProduct[$i]["extra_fields_search"] = strip_tags($obj['extra_fields_search']);
$i++;
}
if($arrayProduct[0]["id"] ==null){
print 0;
}else{
print json_encoder($arrayProduct);
}
function json_encoder($a=false)
{
if (is_null($a)) return 'null';
if ($a === false) return 'false';
if ($a === true) return 'true';
if (is_scalar($a))
{
if (is_float($a))
{
// Always use "." for floats.
return floatval(str_replace(",", ".", strval($a)));
}
if (is_string($a))
{
// static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '', '\\t', '', '', '\\f', '\"'));
return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
}
else
return $a;
}
$isList = true;
for ($i = 0, reset($a); $i < count($a); $i++, next($a))
{
if (key($a) !== $i)
{
$isList = false;
break;
}
}
$result = array();
if ($isList)
{
foreach ($a as $v) $result[] = json_encoder($v);
return '[' . join(',', $result) . ']';
}
else
{
foreach ($a as $k => $v) $result[] = json_encoder($k).':'.json_encoder($v);
return '{' . join(',', $result) . '}';
}
}
mysql_close();
?>
Codice:
<p><a class="jcepopup" href="images/prodotti/accessori-ed-utensili/prolunghe/interne/5603int.jpg" target="_blank"><img src="images/prodotti/accessori-ed-utensili/prolunghe/interne/5603int.jpg" alt="" /></a></p> <table class="responsive"> <thead> <tr> <td colspan="2">AZIENDA</td> <td>MICHELIN</td> <td>L</td> </tr> </thead> <tbody> <tr> <td>n.</td> <td>code</td> <td>n.</td> <td>mm</td> </tr> </tbody> <tfoot> <tr> <td>5600</td> <td>METALFLEX 75</td> <td>980 R75</td> <td>75</td> </tr> <tr> <td>5601</td> <td>METALFLEX 105</td> <td>795 R105</td> <td>105</td> </tr> <tr> <td>5602</td> <td>METALFLEX 125</td> <td>1325 R125</td> <td>125</td> </tr> <tr> <td>5603</td> <td>METALFLEX 140</td> <td>1145 R140</td> <td>140</td> </tr> <tr> <td>5616</td> <td>METALFLEX 160</td> <td>1488 R160</td> <td>160</td> </tr> <tr> <td>5604</td> <td>METALFLEX 180</td> <td>723 R180</td> <td>180</td> </tr> <tr> <td>5606</td> <td>METALFLEX 210</td> <td>743 R210</td> <td>210</td> </tr> <tr> <td>5608</td> <td>METALFLEX 270</td> <td>940 R270</td> <td>270</td> </tr> <tr> <td>5615</td> <td>METALFLEX 335</td> <td>720 R335</td> <td>335</td> </tr> <tr> <td>5611</td> <td>METALFLEX 370</td> <td>873 R370</td> <td>370</td> </tr> <tr> <td>5612</td> <td>METALFLEX 430</td> <td>797 R430</td> <td>430</td> </tr> <tr> <td>5613</td> <td>METALFLEX 475</td> <td>721 R475</td> <td>475</td> </tr> </tfoot> </table> Qualche idea su come potrei fare?? Grazie |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Nov 2010
Città: Londra
Messaggi: 620
|
"l'orrore" cit.
non saprei di preciso come farlo in php, mentre dal client side credo sia più semplice con jQuery |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 17:13.



















