strripos does not seems to work for me - strpos

I'm trying to use strripos in fact I try to add a parameter to an URL fro translating my website,
the fact is that not over the url requested does have a parameter so I can not to everytime &lang=en but I need to do ?lang=en
I've tried to do a function using strripos like that
/**
* Fonction qui renvoit le bon caractère en tant que paramètre
* #param type $string
* #return string
*/
function checkParam($string){
$lookfor = "?";
$position = strripos($string, $lookfor);
if($position === true){
return "&";
}else{
return "?";
}
}
I have for example that url : $url_courante = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
which give me something like that
www.mydomain.eu/index.php?p=nos-produits
but it does not recognize the ? in that string, so it returns always to me false and I can not set properly my lang to the url.
Anykind of help will be much appreciated.

strripos returns an integer or false, not true. Switch the condition around and check for false.
function checkParam($string){
$lookfor = "?";
$position = strripos($string, $lookfor);
if($position === false){
return "?";
}else{
return "&";
}
}
Also, there is no need for strripos because you are only checking to see if the string exists. It does not matter if you find the first or last. Case is not an issue when you are searching for a question mark, either. I would use strpos instead.

Related

Issue trying to convert HTML to Ajax

I got this in the video script that I use for my website (Videos Page Layout):
<div class="video-views pull-left">
{$videos[i].viewnumber|kilo} {if $videos[i].viewnumber == '1'}{t c='global.view'}{else}{t c='global.views'}{/if}
</div>
Related videos section uses Ajax for videos page layout generated with the "show more" button.
My problem is: I don't know how to convert the "kilo" function in Ajax {$videos[i].viewnumber|kilo}. I attempted a few things but with no result.
$code[] = '<div class="video-views pull-left">';
$views = ($video['viewnumber'] == '1') ? $lang['global.view'] : $lang['global.views'];
$code[] = $video['viewnumber']. ' '.$views;
$code[] = '</div>';
Kilo is not a modifier that is included in the Smarty distribution, but if you have access to the code on your site, you can extract the code from the plugin file, it is most likely in smarty/libs/plugins/modifier.kilo.php.
It looks like you're working in PHP to construct a response to an AJAX request, so you can just pull that code out and re-use it.
If you do not have access to the modifier file, you can just recreate the formatting on your own. Judging by the context, it's something simple like:
<?php
/**
* Format integers into human readable strings indicating number of thousands
* Example: 1200 -> 1.2K
* #param int $value
* #return string
*/
function kilo(int $value): string
{
// If the value is less than 1000, just return it
if($value < 1000)
{
return $value;
}
/*
* If the value is evenly divisible by 1000, we want to show a whole number,
* otherwise format it as a single precision float. Add "K" string literal
* to indicate thousands
*/
$formatString = ($value % 1000) ? '%.1fK':'%dK';
// Divide value by 1000
$value /= 1000;
// Return the formatted string
return sprintf($formatString, $value);
}
// Define some test data and echo the formatted values
$testViewCounts = [1, 123, 1230, 12300, 123000];
foreach($testViewCounts as $views)
{
// If only one view, do not pluralize
$label = ($views == 1) ? 'view':'views';
echo kilo($views).' '.$label.PHP_EOL;
}

Natural sorting Ajax Datatables in Codeigniter

Struggling to figure out how to set natural sorting in AJAX Datatables using Codeigniter Active record.
The field that should be sorted has, in most cases, just digits...in other cases a string, so the MySQL table field is set as VARCHAR.
I need to srt naturally the field to be displayed in Datatables.
The Active record Codeigniter query is the following.
function list_all($limit,$start,$col,$dir)
{
$this->rmi_db->select ("
$this->table_dev.id,
$this->table_dev.fl,
$this->table_dev.mm,
$this->table_dev.batch,
$this->table_dev.n,
$this->table_dev.ditta,
$this->table_dev.tipo,
$this->table_dev.costruzione,
$this->table_dev.motori,
$this->table_dev.nc,
$this->table_dev.serie,
$this->table_dev.ca,
$this->table_dev.consegna,
$this->table_dev.matr_usaf AS usaf,
$this->table_dev.matr_usn AS usn,
$this->table_dev.matr_caf AS caf,
$this->table_dev.matr_raf AS raf,
$this->table_dev.codici,
$this->table_dev.note,
$this->table_dev.reg_civili,
$this->table_dev.matricola_civ,
$this->table_dev.prima_reg,
$this->table_dev.n_contratto,
$this->table_dev.data_contratto,
$this->table_dev.importo_contratto,
$this->table_dev.note_contratto,
$this->table_dev.f29,
$this->table_dev.f30,
");
$this->rmi_db->from("$this->table_dev");
$this->rmi_db->where("$this->table_dev.mm !=", "");
$this->rmi_db->limit($limit, $start);
$this->rmi_db->order_by($col, $dir);
$query = $this->rmi_db->get();
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return null;
}
}
The mm field should be sorted naturally. I have no idea how and if it's possible to fix the issue.
I tried the solution in this discussion solutions, the Bin way, but the select doesn't work properly ( got 500 server error)
Thanks a lot for any help
Using Solution, Try below. It should work but not tested.
function list_all($limit,$start,$col,$dir)
{
$this->rmi_db->select ("
$this->table_dev.id,
$this->table_dev.fl,
$this->table_dev.mm,
$this->table_dev.mm, CAST($this->table_dev.mm as SIGNED) AS casted_column,//changed
$this->table_dev.batch,
$this->table_dev.n,
$this->table_dev.ditta,
$this->table_dev.tipo,
$this->table_dev.costruzione,
$this->table_dev.motori,
$this->table_dev.nc,
$this->table_dev.serie,
$this->table_dev.ca,
$this->table_dev.consegna,
$this->table_dev.matr_usaf AS usaf,
$this->table_dev.matr_usn AS usn,
$this->table_dev.matr_caf AS caf,
$this->table_dev.matr_raf AS raf,
$this->table_dev.codici,
$this->table_dev.note,
$this->table_dev.reg_civili,
$this->table_dev.matricola_civ,
$this->table_dev.prima_reg,
$this->table_dev.n_contratto,
$this->table_dev.data_contratto,
$this->table_dev.importo_contratto,
$this->table_dev.note_contratto,
$this->table_dev.f29,
$this->table_dev.f30,
");
$this->rmi_db->from("$this->table_dev");
$this->rmi_db->where("$this->table_dev.mm !=", "");
$this->rmi_db->limit($limit, $start);
$this->rmi_db->order_by($col, $dir);
$this->rmi_db->order_by('casted_column', 'ASC'); // changed
$this->rmi_db->order_by($this->table_dev.mm, 'ASC'); // changed
$query = $this->rmi_db->get(); //changed
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return null;
}
}
comment if you face any issue

ActionScript "?:" conditional operator and return in function

I have function :
public static function validate(value:*):Boolean
{
...
if(field_counter < FIELD_LIMIT){
field_counter++;
}else{
return false;
}
return true;
}
I want to make it one line, but it shown Syntax error on "return false":
field_counter < FIELD_LIMIT ? field_counter++ : return false;
If field_counter is not a negative number, you can forget that if and compute everything in a single instruction:
public static function validate(value:*):Boolean
{
return (field_counter < FIELD_LIMIT && ++field_counter)
}
The instruction ++field_counter will not be executed if field_counter is not lower than FIELD_LIMIT.
Edit
Here's a preview:
http://wonderfl.net/c/c7lA
Why make it hard on yourself and any other developer when you can make it simple?
if(field_counter >= FIELD_LIMIT)
return false;
field_counter++;
return true;
You can try working around this by testing something about the field_counter (not the nicest way but should work):
return (field_counter < FIELD_LIMIT ? (field_counter++!=null) : false);
You are attempting to stuff a return statement into a conditional. The trick is, the ?: operator returns a value, so you can do say x= y>z ? 1 : z-y; and return statement does not return a value in terms of an expression. You'd better leave the original if statement intact.

What does this line of Actionscript do?

I'm looking at the as3delaunay library and most of the code is clear to me. This part is not, however (note the line that I put preceded with an arrow):
public function circles():Vector.<Circle>
{
var circles:Vector.<Circle> = new Vector.<Circle>();
for each (var site:Site in _sites)
{
var radius:Number = 0;
var nearestEdge:Edge = site.nearestEdge();
=======>> !nearestEdge.isPartOfConvexHull() && (radius = nearestEdge.sitesDistance() * 0.5);
circles.push(new Circle(site.x, site.y, radius));
}
return circles;
}
For reference, isPartOfConvexHull() is found in Edge.as and looks like this:
internal function isPartOfConvexHull():Boolean
{
return (_leftVertex == null || _rightVertex == null);
}
What does !nearestEdge.isPartOfConvexHull() do? Does that mean that the radius = nearestEdge.sitesDistance() * 0.5 only executes if false is returned from the call to isPartOfConvexHull()? Does that stop execution of any other code?
It is equivalent to:
if (!nearestEdge.isPartOfConvexHull()) {
radius = nearestEdge.sitesDistance() * 0.5;
}
In the following line:
var b:Boolean = expression1 && expression2;
expression2 will not be evaluated if expression1 is false because we already know the final result: b = false.
Now in the following line:
expression1 && expression2;
The same thing happens except the fact that we are not assigning the result to a variable.
And this is exactly what happens in the line you are asking about where !nearestEdge.isPartOfConvexHull() is the first expression and (radius = nearestEdge.sitesDistance() * 0.5) is the second expression.
To extends #sch answer with some explanations (I didn't knew if editing answer to almost double it was ok).
This is based on lazy execution of the interpreter. If (!nearestEdge.isPartOfConvexHull()) is False then there's no need to execute the second part of the AND statement to know it'll be False, then it's left unexecuted. If it's true the evaluation of the complete statement is needed (and then done) to tell wether or not this boolean is True. So this is equivalent to an if statement.
TMHO this is bad code since it's to much condensed and hard to understand.

Update MySQL Value Through Img Src Variable Including jQuery

Thank-you to all who have helped me over the last few days.. Unfortunately I was working so I couldn't get back to you. I have included some code into what I thought would work, but for some reason the below code will not update in my SQL Database. I will provide the code and it's output if someone could please copy the code and see why it's not working... It's really doing my head in! Haha!
(The connection to the MySQL db + table is working fine).
// admin.php
<a href="#" id="chngeHref" /><img src="<?php echo "image.php?url=" . $row[2]; ?>?tid=<?php echo $row[0]; ?>&opn=<?php echo $row[1]; ?>" id="chnge" /></a>
// image.php?url=image.jpg?tid=3&opn=1
I was advised to do it this way to make it easier for me to pass the variables (tid and opn) through the process.
// update.php
$tid = $_GET['tid'];
$opn = $_GET['opn'];
if ($opn == "0") { $opn = "1"; } elseif ($opn == "1") { $opn = "0"; }
mysql_query("UPDATE catalogue SET opn = $opn WHERE tid = $tid ; ");
mysql_close();
// it's just a simple script to change a variable from 1 to 0 or 0 to 1 where tid = a specific number...
I have my jQuery stuff all tucked away in a lovely little file, because there is alot of it...
// navigate.js
$.extend({
getUrlVars: function() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { vars[key] = value; });
return vars;
}
});
$("#chngeHref").click(function() {
var tid = $.getUrlVars()['tid'];
var opn = $.getUrlVars()['opn'];
$.ajax({
type: "POST",
url: "update.php",
data: "tid="+ tid +"& opn="+ opn,
success: function(){
$('#chnge').fadeTo('slow',0.4);
}
});
});
The .extend code i found on the net which finds the parameter and value of all those in the address line. I THINK this is where my issue might be, because the top code is never actually sending it to the address bar, it's being sent through jQuery to the update.php file.
I can only say thank-you soooo much in advance to anyone who can assist in this.
Phillip.
There are a few issues here bsides the SQL Injection vulnerability Nathan mentions, namely you're POSTing, so you need to use $_POST rather than $_GET to retrieve your variables. Also you have an extra space in the data block, this:
data: "tid="+ tid +"& opn="+ opn,
should be:
data: "tid="+ tid +"&opn="+ opn,
or a bit cleaner using object notation (so it also gets properly encoded):
data { tid: tid, opn: opn },
For the SQL Injection issue, instead of this:
mysql_query("UPDATE catalogue SET opn = $opn WHERE tid = $tid ; ");
At the very least escape the values, like this:
$tid = mysql_real_escape_string($_POST['tid']);
$opn = mysql_real_escape_string($_POST['opn']);
Or, go the parameterized query route, which is what I'd prefer.