codeigniter blank URI segment - php

i have the following URI:
http://mysite/myapp/index.php/test/testURL/CAN-q1/300-48/fa1/59//can-a2
You'll notice that the 7th URI segment is not set.
I need to test for these types of scenarios.
I've tried a few different things but i can't seem to get it to work. What's happening is segment 8 is being assigned as segment 7.
Here's some test code I've been playing with this morning:
echo '3 - '. $this->uri->segment(3);
echo '<BR>';
echo '4 - '. $this->uri->segment(4);
echo '5 - '. $this->uri->segment(5);
echo '<BR>';
echo '6 - '.$this->uri->segment(6);
echo '<BR>';
$test = $this->uri->segment(7,0);
echo '7 is: '.$test;
echo '<BR>';
echo '8 - '. $this->uri->segment(8);
echo '<BR>';
And the results come back as:
3 - CAN-q1
4 - 300-48
5 - fa1
6 - 59
7 is: can-a2
8 -
Based on reading the codeigniter manual, i would have expected that segment 7 would contain a 0 or whatever i passed as the second parameter in the uri->segment() method.
Can you tell me where I'm going wrong?
Thanks.

I just decided to pass a space instead of an empty URI, based on comments that others have made that you can't have a empty segment.
This seems to have resolved the problem.
Thanks to everyone who commented.

Related

$variable after <br> is not showing in echo in php

I'm learning basics in php, and I was trying to add multiple arguments in echo command. But the variable after <br> is not showing.
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
echo "<br> ".$number1+$number2;
and the output should be:
number 1 is: 10
number 2 is: 20
30
But the output is:
number 1 is: 10
number 2 is: 2020
So what's the error?
used this code
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
$total= $number1+$number2;
echo "<br> ".$total;
?>
The output will be:
number 1 is: 10
number 2 is: 20
30
The other answers just state a solution, this answer explains whats happening and how to prevent the unexpected behavior in 2 ways.
The dot operator has the same precedence as + and -.
Considering
$number1 = 10;
$number2 = 20;
echo "<br> ".$number1+$number2;
The dot you've used is a string operator, not a numeric operator.
What's happening:
"<br>" and 10 are concatenated with the dot operator to "<br>10".
"<br>10" is added to $number2 (20) with the numeric + operator.
Non-empty, non-numeric strings are converted to 0. Meaning "<br>10" = 0.
0+20 results in 20 which makes line 3: echo 20;
This can be solved by changing the precedences by using brackets echo "<br> ". ($number1 + $number2); or the less seen option, by passing more arguments to the echo language construct: echo "<br> ", $number1 + $number2; (Note the comma instead of a dot). Each argument will be evaluated first before outputting them all together.
Personally I use the second option (multiple arguments) in cases like this.
Just add the braces to the sum operation.
$number1=10;
echo "number 1 is: ".$number1."<br>";
$number2=20;
echo "number 2 is: ".$number2;
echo "<br> ".($number1+$number2);
The output will be:
number 1 is: 10
number 2 is: 20
30
You should revise code within bracket
echo "<br> ". ($number1 + $number2);
to get the result you want.
Reason: each operation has precendence level
Get reference: http://interactivepython.org/runestone/static/pythonds/BasicDS/InfixPrefixandPostfixExpressions.html

printf in CLI php printing extra numbers at end of line

I have a php (v. 7.0.16) script running on the command line with this:
$ct = 1;
foreach($sort_this as $cur_site_id => $dx){
$cur_location = $locations[$cur_site_id]['location'];
$msg = "\033[A\33[2K\r" . 'getting data for %25s (' . $cur_site_id . ') store: %7s %01.2f' . "\n";
echo printf($msg, $cur_location, ($ct . ' / ' . count($sort_this)), number_format((($ct / count($sort_this)) * 100), 2));
$ct++;
}
This loop runs about 40 iterations. The printf statement works with 1 small problem. On the line after the "getting data" line I see a number that increments from 7x-7y as it runs (sometimes it starts at 71, sometimes at 77, etc.). I can't figure out what's causing this number to be printed. So when the loop starts I see something like this on the screen:
getting data for DENVER (531) store: 42 / 42 0.00
77
and when it finishes something like this:
getting data for SEATTLE (784) store: 42 / 42 100.00
79
I found how to print to the same line and clear the data here:
Erase the current printed console line
Is there a way to prevent the 7x codes from showing? Also, what are they?
The problem is on this line:
echo printf(...)
printf() generates a string using its arguments (format and values) and prints it. It returns the number of printed characters (the length of the string it generated).
Your code then passes this value to echo that prints it. This is the source of the extra number in the range of 70-77.
You should either remove the echo or use sprintf() instead of printf().
sprintf() generates the string the same was printf() does but it doesn't print it; it returns it and it is passed as argument to echo that displays it.
printf() returns the length of the outputted string.
See PHP docs:
Return Values: Returns the length of the outputted string.
Just remove echo fom that line.

strpos() doesn't seem to be recognizing my the string I am using

I have the following string:
CAE33D8E804334D5B490EA273F36830A9849ACDF|xx|yy|46|13896|9550
which in the code below corresponds to $track_matches[0][0].
The only constant-length field is the first (CAE33D8E804334D5B490EA273F36830A9849ACDF), which is 40 characters long. I am trying to get the values xx and yy which are an unknown length and value along with the rest of the column.
So I am trying something like this:
$seperator= '|';
$end_seed= strpos($track_matches[0][0], $seperator, 41 );
$seeders[$i] = substr($track_matches[0][0], 41, $end_seed - 41);
$end_leech= strpos($track_matches[0][0], $seperator, $end_seed +1 );
echo "end_seed" . $end_seed . " end_leach: " . $end_leech;
$leechers[$i] = substr($track_matches[0][0], $end_seed +1, $end_leech - $end_seed - 1);
The problem I am getting is the line $end_leech= doesn't seem to work properly (and doesn't recognize the $seperator) and retuns the entire line ($track_matches[0][0]) as it's value when echo'd while $end_seed returns the proper value. ... so what's going on why is this happening? howw do i fix it?
try:
$temp = explode("|", $track_matches[0][0]);
That will return an array and you can then reference the vars as $temp[1] (xx) and $temp[2] (yy)
try :
$myString="CAE33D8E804334D5B490EA273F36830A9849ACDF|xx|yy|46|13896|9550";
$splitString=explode('|',$myString);
$xx=$splitString[1];
$yy=$splitString[2];
of course you can replicate manually with strpos, substr etc but will take more effort

number from $_GET not being read into simplexml xpath query using php

THIS WORKS:
1 $number = 2;
2 $allofit = simplexml_load_file("thexmlfile.xml");
3 $thebook = $allofit -> booklist[$number] -> abook;
4 echo $thebook;
THIS FOLLOWING DOES NOT WORK:
But if I want to read $number from a from with method GET I set
6 $number=$_GET[thenumber]; // $number=2 from the form//
7 echo $number; // and properly shows $number=2 --*/
8 $allofit = simplexml_load_file("thexmlfile.xml");
9 $thebook = $allofit -> booklist[$number] -> abook;
10 echo $thebook;
The echo on line 10 reports nothing (no error, just blank space in the html) even though I can successfully echo $number in line 7... so its being set, but just not picked up in line 9.. although the equivalent in line 3 works (!).
Any ideas folks?
Thanks in advance
J
$number=$_GET[thenumber];
At this stage $number is a string.
Do you need to coerce it into an integer? Probably with intval? http://php.net/manual/en/function.intval.php
Try:
$number=intval($_GET[thenumber]);
PHP should convert your "2" to a 2 without a problem. My guess is that you're missing quotes: $number=$_GET[thenumber]. Try $number=$_GET['thenumber'];

PHP variable not incremented

I have a small code as part of a huge file as follows:
if(($lastLogTime + $logOffset)>= $text1)
{
echo $text1.'<br>';
$uptime=$uptime + (($text1 - $lastLogTime)/60000);
echo ($text1 - $lastLogTime).'<br>';
fwrite($fd, $uptime.',');
echo $uptime.'<br><br>';
$lastLogTime = ($lastLogTime + 1800000);
echo $lastLogTime.' ME <br>';
}
The weird part is the output for the final $lastLogTime is NOT getting added by the 1800000 OR a variable called $logInterval = 1800000 which was initialized earlier.
The output is
1298083876650 - i.e lastLogtime
1298083877661 - text1
1011 - the difference
0.01685 - uptime
1298085676650 ME - damn ! doesn't get added by 1800000
NEW EDIT :
I solved it ! bad answers guys.. Thanks for the time anyways.
Am i the only one facing weird shhit like this ?
I suspect it has something to do with this:
What's the maximum size for an int in PHP?
Check out the php date function. Though working with dates can be tedious, converting everything to seconds normally isn't the best approach.
http://php.net/manual/en/function.date.php

Categories