There are a lot of syntax highlighters out there but something I have not seen is one that supports highlighting query strings!
I'm looking for something to use when documenting my API and well being an API there are a lot of query strings involved. so. What good javascript or PHP syntax highlighters are there that support query strings?
I have looked into Google code prettify, highlight.js, prismjs and a few others but none of them seem to be able to highlight this:
/oauth/authorize?client_id=wG2X7q1qz74zdSbgiFkyL5JFOeloQwg2opfrPfaJ&response_type=code&redirect_uri=https%3A%2F%2Fmyapplication.com%2Foauth&scope=account%2Ccompetition%2Cvideos&state=d41d8cd98f00b204e9800998ecf8427e
Which should highlight all the keys on one colour, and all the values in another with the &?= being a different colour again... something like:
As far as i am concerned all characters like '?', '&' and '=' should be replaced in names / values in a proper URL. This makes it super easy to parse.
So why not invent something on your own:
function parseQuery(query){
parsed = query.split('?');
if (parsed.length > 1) {
parsed[1] = parsed[1].split('&');
for(i in parsed[1]) {
parsed[1][i] = parsed[1][i].split('=');
}
}
retStr = '<span class="path">' + parsed[0] + '</span>';
if (parsed.length > 1) {
retStr += '<span class="qm">?</span>';
first = true;
for(i in parsed[1]) {
if (first) {
first = false;
} else {
retStr += '<span class="amp">&</span>';
}
retStr += '<span class="name">' + parsed[1][i][0] + '</span>';
if (parsed[1][i].length > 1) {
retStr += '<span class="eq">=</span>' +
'<span class="value">' + parsed[1][i][1] + '</span>';
}
if (parsed[1][i].length > 2) {
for (var j = 2; j < parsed[1][i].length; j++) {
retStr += '<span class="eq">=</span>';
retStr += '<span class="error">' + parsed[1][i][j] + '</span>';
}
}
}
}
if (parsed.length > 2) {
for (var i = 2; i < parsed.length; i++) {
retStr += '<span class="qm">?</span>';
retStr += '<span class="error">' + parsed[i] + '</span>';
}
}
return retStr;
}
http://jsfiddle.net/YqrpV/1/
Related
basically I need help to display data in a table using the "td" and "tr" tags.
html += '<table class="table table-striped">';
for(var i in value){
if(!number_regex.test(i)){
html += '<tr>';
html += '<td>' + i + '</td>';
if(i == 'image_link'){
html += '<td> : ' + '<img src=' + value.image_link + ' alt>' + '</td>';
}
else{
html += '<td> : ' + value[i] + '</td>';
}
html += '</tr>';
}
}
html += '</table><br/>';
This is my current code where the value is an object. This outputs one "td" in each "tr". I'm trying to align 2 "td"s in one "tr". However, this cannot be accomplished through next(i) or (i+1) as it isn't a number.
This is what I have tried so far to no avail:
html += '<td>' + next(i) + '</td>';
if(next(i) == 'image_link'){
html += '<td> : ' + '<img src=' + value.image_link + ' alt>' + '</td>';
}
else{
html += '<td> : ' + value[next(i)] + '</td>';
}
and:
html += '<table class="table table-striped">';
html += '<tr>';
for(var i in value){
if(!number_regex.test(i)){
html += '<td>' + i + '</td>';
if(i == 'image_link'){
html += '<td> : ' + '<img src=' + value.image_link + ' alt>' + '</td>';
}
else{
html += '<td> : ' + value[i] + '</td>';
console.log(data);
}
for(var counter = 0; counter <= 8; counter++){
//8 because there are 9 fields in the query I want to display
if(counter % 2 == '0'){
html += '</tr>';
html += '<tr>';
}
}
}
Any idea guys?
These are the additional information that some of you requested:
A picture of the table I have atm:
https://www.dropbox.com/s/kmhkwturtmcxovh/lala.png
and as for the html, this is a javascript syntax, which is why I'm using html in this manner.
Take a variable and place it in the loop, which does the job of ending the rows by adding </tr>...something like :
This is what you have right now :
html += '</tr>';
amend it to something like :
$var = 0; /* initiaize it somewhere outside for(var i in value) */
if(++$var < 2)
{
/* do not echo </tr> if 2 td's haven't been added */
}
else /* 2 td's have been added, end this row */
{
$var = 0; /* reset counter*/
html += '</tr>'; /* end rows */
}
I'm trying to built a search for a website and i'm having trouble in making it work right.
I want to get the query from POST (or link using GET) after submiting the form. post it to a php and return the json back to results div
.
I tried to post it with ajax but i cant make it right.
The url that returns json must have all the query values (cars.php?company_id=1&model_id=1&car_category=compact&priceFrom=min&priceTo=max&kmFrom=all&kmTo=all&yearFrom=all&yearTo=all) or retuns a specific query (with all the results from table)
$(document).ready(function () {
$("form1#submit").submit(function () {
$.ajax({
type: 'POST',
url: './system/feeds/cars.results.php',
data: $(this).serialize(),
success: function () {
$.getJSON('./system/feeds/cars.results.php', function (json) {
var output = '';
for (var i = 0; i < json.car.length; i++) {
output += '<div class="grid_12">';
output += '<div class="block3">';
output += '<img src="images/' + json.car[i].car_image + '" alt="" class="img_fleft img-rounded">';
output += '<div class="wrapper border_block">';
output += '<div class="marg2">';
output += '<div class="grid_6 alpha">';
output += '<strong>' + json.car[i].car_company + ' ' + json.car[i].car_model + ' ' + json.car[i].car_cc + '</strong><br>' + json.car[i].car_category + ', ' + json.car[i].car_combustible + ', ' + json.car[i].car_bhp + ' bhp</br></br>';
output += '<hr>';
output += '<p>' + json.car[i].car_description + '</p>';
output += 'read more';
output += '</div>';
output += '<div class="grid_3 omega">';
output += '<h4>Χρονολογία:</h4> <a class="btn">' + json.car[i].car_month + '/' + json.car[i].car_year + '</a><br>';
output += '<h4>Χιλιόμετρα:</h4> <a class="btn">' + json.car[i].car_km + '</a><br>';
output += '<h4>Τιμή:</h4> <a class="btn">€ ' + json.car[i].car_price + '</a><br>';
output += '</div>';
output += '</div>';
output += '</div>';
output += '</div>';
output += '</div>';
output += '<div class="clear"></div>';
}
$('#cars-results').html(output);
$('#car_counter').html(json.car.length);
});
}
});
});
});
The json php returns results when u POST a query like car.results.php?company_id=1&model_id=1
car.results.php - Demo code (not basic yet)
<? include ('../config.php');
if (isset($_REQUEST['company_id']) && isset($_REQUEST['model_id'])) {
$company_id = $_REQUEST['company_id'];
$model_id = $_REQUEST['model_id'];
$car_category = $_REQUEST['car_category'];
$priceFrom = $_REQUEST['priceFrom'];
$priceTo = $_REQUEST['priceTo'];
$kmFrom = $_REQUEST['kmFrom'];
$kmTo = $_REQUEST['kmTo'];
$yearFrom = $_REQUEST['yearFrom'];
$yearTo = $_REQUEST['yearTo'];
$result = mysql_query("SELECT * FROM `cars` WHERE `company_id` = '$company_id' AND `model_id` = '$model_id'") or die("Couldn’t get data from database");
} else {
$result = mysql_query("SELECT * FROM `cars` ORDER BY `datetime` DESC LIMIT 3") or die("Couldn’t get data from database");
}
// Create JSON
$json = array();
while($row = mysql_fetch_array($result)) {
$car = array('car_image' => $row['car_image'],
'car_link' => "car.php?id=".$row['car_id'],
'car_company' => $row['company_id'],
'car_model' => $row['model_id'],
'car_cc' => $row['car_cc'],
'car_category' => $row['car_category'],
'car_combustible' => $row['car_combustible'],
'car_bhp' => $row['car_bhp'],
'car_description' => $row['car_description'],
'car_month' => $row['car_month'],
'car_year' => $row['car_year'],
'car_km' => $row['car_km'],
'car_price' => $row['car_price']);
array_push($json,$car);
}
// header('Content-type: application/json');
echo "{\"car\":".json_encode($json)."}";
ob_flush(); ?>
Any suggestions ?
My phpcode is
<?php for($i = 1; $i < 5; $i++) { ?>
<div class="upperBoxContainer" id="test1<?php echo $i; ?>">
<img id="test<?php echo $i; ?>" src="images/midimages/<?php echo $i; ?>.jpg">
</div>
<?php } ?>
And my javascript code is
jQuery(document).ready(function() {
for (var i = 1; i < 5; i++) {
alert('images/midimageshover/' + i + '.jpg');
$('#test1' + i).hover(function() {
$('#test' + i).attr('src', 'images/midimageshover/' + i + '.jpg');
});
$('#test1' + i).mouseout(function() {
$('#test' + i).attr('src', 'images/midimages/' + i + '.jpg');
});
}
});
There is some error due to this code is not work. can you please find this.
Thanks in advance
Enclose your events in a Immediately-Invoked Function Expression (IIFE)
jQuery(document).ready(function(){
for (var i = 1; i < 5; i++) {
(function(i){
$('#test1'+i).hover(function(){
$('#test' + i).attr('src', 'images/midimageshover/' + i + '.jpg');
});
$('#test1'+i).mouseout(function(){
$('#test' + i).attr('src', 'images/midimages/' + i + '.jpg');
});
})(i);
}
});
Your code is rewriting the value of $('#test1'+i) until it reaches 4, so at the end of the loop you'll only have $('#test14').
The code above create different scope by using the IIFE hence $('#test1'+i) won't be overwritten, instead you'll have $('#test11'), $('#test12'), $('#test13'), $('#test14')
Here is a demo to demonstrate the difference with your initial code
I'm trying to implement something like facebook's like widget which says something like:
You, Name1, Name2 and 20 other people like this
I fetched all my data to be able to display this HTML, but I can't seem to find the right algo to form the HTML string.
My main problem is that I don't know when to put the and string or the , (comma) string. If I just had to put names, it would work, but the problem is that the You string always has to be first.
I'm going to paste my code here and the output I'm getting for some special cases (it's PHP).
$current_user = 0;
$html = "";
$count = count($result);
$key = 0;
foreach($result as $liked){
if($key + 1 > $limit)
break;
if($liked->uid == $user->uid){
$current_user = 1;
continue;
}
$html .= "<a href='".$liked->href."'>".$liked->name."</a>";
if($key < $count - 2)
$html .= ", ";
elseif($key == $count - 2 && $key + 1 != $limit)
$html .= " and ";
$key++;
}
if($current_user){
$userHtml = "You";
if($count > 2)
$userHtml .= ", ";
elseif($count > 1)
$userHtml .= " and ";
$html = $userHtml.$html;
}
$html = "♥ by ".$html;
if($count > $limit){
$difference = $count - $limit;
$html .= " and ".$difference." ".format_plural($difference,"other","others");
}
return $html;
And in the special case where the current user is the last one to like this, it will show:
♥ by You, admin, edu2004eu and
Notice the and word doesn't have anything after it, because You should have been after it, but I put it at the beginning. Any help? I just need the logic, not the actual code.
You can try something like this:
$likedBy = array('admin', 'eduard', 'jeremy', 'someoneelse');
// check if I like it and if so move me to the front
if (in_array($currentUsername, $likedBy)) {
$me = array_search($currentUsername, $likedBy);
unset($likedBy[$me]);
array_unshift($likedBy, 'You');
}
// remove anything after the limit
$extra = array_splice($likedBy, 3);
// the comma list
$html = implode(', ', $likedBy);
// any extras? if so, add them here, if not rewrite the list so
// it's "You, Eduard and admin"
if (!empty($extra)) {
$html .= ' and '.count($extra);
} else {
$lastguy = array_splice($likedBy, 1);
$html = implode(', ', $likedBy).' and '.$lastguy;
}
$html .= ' like this';
Eduard,
You can fix this simply by putting $key++; at the top of the loop and taking out all the places where you have $key + 1 in the loop.
I think what's happening is that $key + 1 is assuming that there is a current user.
This line would also not display the current user if they are not in the first $limit number of entries
if($key + 1 > $limit)
break;
You can fix this by putting this after the code that looks for the current user.
In Java (I know, but its what I had running) it would look something like:
List<String> users = Arrays.asList("Brian","Tom","Jack","John");
int key = 0;
String html = "";
String currentUser = "Brian";
int limit = 3;
boolean foundCurrentUser = false;
for (String user : users) {
key ++;
if (currentUser == user) {
foundCurrentUser = true;
continue;
}
if (key > limit) {
continue;
}
html += user;
if (key < users.size() - 1) {
html += ",";
} else if (key == users.size() - 1 && key != limit) {
html += " and ";
}
}
if (foundCurrentUser) {
String userHTML = "You";
if (key > 2) {
userHTML += ", ";
} else if (key == 1) {
userHTML += " and ";
}
html = userHTML + html;
}
html = "Likeed by " + html;
if (users.size() > limit ) {
html += " and 3 other people";
}
System.out.println(html);
I have this php code
$jsonArray = array();
$sql = "SELECT ID,CLIENT FROM PLD_SERVERS";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$jsonArray[] = array('id'=>$row['ID'],'client'=>$row['CLIENT']);
}
echo json_encode($jsonArray);
And this js
function autosearchLoadServers()
{
$.post("php/autosearch-load-servers.php",function(data){
var toAppend = "";
for(var i = 0; i < data.length; i++){
toAppend += '<option value = \"' + data[i].id + '\">' + data[i].client + '</option>';
}
$("#serverSelect").empty();
$("#serverSelect").html(toAppend);
});
}
The problem is that i get only undefined values. How can this be? The values are in the JSON, i checked using firebug in mozilla so there has to be something with the data variable but i can't understand what. I tried different ways and no results.
Try specifying the datatype in the post call like this:
$.post("php/autosearch-load-servers.php",function(data){
var toAppend = "";
for(var i = 0; i < data.length; i++){
toAppend += '<option value = \"' + data[i].id + '\">' + data[i].client + '</option>';
}
$("#serverSelect").empty();
$("#serverSelect").html(toAppend);
}, "json");