I am working on my first program written in PHP. This is a basic question. I am trying to print a string stored in an array. I have two arrays. One array, $content, stores a number at $content[$i][15] (i am using arrays in arrays, and looping through it with a for loop). Lets say this number is 3. I now want to access a string in another array, called $type. This string is at position $type[3]. Logically, I think, $type[3] should print the same thing as $type[$content[$i][15]]. But, when I call $type[$content[$i][15]], it doesn't print anything, but when i print $type[3] it works fine. Is this type of call not allowed in PHP?
$type = array();
$typetemp = readfile("type.csv");
foreach ($typetemp as $sa){
$type[$sa[0]] = $sa[1];
}
$content = readfile("content.csv");
for($i=0; $i<sizeof($content); $i++){
if($content[$i][15] == 3){
if($content[$i][4] == 1){
$temp = $content[$i][15];
echo "id is " . $content[$i][0] . "title is " . $content[$i][1] . "introtext is " . $content[$i][2] . "restoftext is " . $content[$i][3] . "visible is " . $content[$i][4] . "category is " . $content[$i][5] . "creation_date is " . $content[$i][6] . "author is " . $content[$i][7] . "img is " . $content[$i][8] . "ordering is " . $content[$i][9] . "rank is " . $content[$i][10] . "vid is " . $content[$i][11] . "start_date is " . $content[$i][12] . "stop_date is " . $content[$i][13] . "event_date is " . $content[$i][14] . "type is " . $content[$i][15] . "thumb is " . $content[$i][16] . "type name is " . $type[$temp] . "<br/>";
}
}
}
function readfile($filename) {
$line_of_text = array();
$file_handle = fopen($filename, "r");
while (!feof($file_handle) ) {
array_push($line_of_text, fgetcsv($file_handle, 1024));
}
fclose($file_handle);
return $line_of_text;
}
You are missing a $ sign before content:
$type[$content[$i][15]]
This accesses the value in $type with index $content[$i][15]
Related
I am trying to do some Diffie-Hellman which works fine with smaller generator, mod and private keys. But when the numbers get to large it just outputs zero. I've looked at other posts with no real solution. Could it be configured in php.ini?
$gen = 877;
$mod = 80182923;
$saltA = 517;
$saltB = 1517;
echo "calculation of Ga og Gb<br>";
echo "<br>user A shareable value<br>";
$calculatedSecretKeyA = ($gen**$saltA) % $mod;
echo $gen . "^" . $saltA . " mod " . $mod . " = " . $calculatedSecretKeyA;
echo "<br>user B shareable value<br>";
$calculatedSecretKeyB = ($gen**$saltB) % $mod;
echo $gen . "^" . $saltB . " mod " . $mod . " = " . $calculatedSecretKeyB;
echo "<br>user As calc of secret key <br>";
$calcKeyA = ($calculatedSecretKeyB**$saltA) % $mod;
echo $calculatedSecretKeyB . "^" . $saltA . "" . " mod " . $mod . " = " . $calcKeyA;
echo "<br><br>user Bs calc of secret key <br>";
$calcKeyB = ($calculatedSecretKeyA**$saltB) % $mod;
echo $calculatedSecretKeyA . "^" . $saltB . "" . " mod " . $mod . " = " . $calcKeyB;
echo "<br><br>bruteforcer A calc of secret key <br>";
$calcKeyB = ($calculatedSecretKeyA**7) % $mod;
echo $calculatedSecretKeyA . "^" . 7 . "" . " mod " . $mod . " = " . $calcKeyB;
You can use GMP library for this (https://www.php.net/manual/en/ref.gmp.php):
echo "calculation of Ga og Gb<br>";
echo "<br>user A shareable value<br>";
$calculatedSecretKeyA = gmp_mod ( gmp_pow ($gen, $saltA), $mod);
echo $gen . "^" . $saltA . " mod " . $mod . " = " . $calculatedSecretKeyA;
Output:
calculation of Ga og Gbuser A shareable value877^517 mod 80182923 = 79127908
I'm trying to show a list of all nearby weather stations.
I have the code:
$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");
$parsed_json = json_decode($json_string);
$stations = $parsed_json->{'location'}->{'nearby_weather_stations'}->{'pws'}->{'station'};
$count = count($stations);
for($i = 0; $i < $count; $i++)
{
$station = $stations[$i];
if (count($station) > 1)
{
echo "City: " . $station->{'city'} . "\n";
echo "State: " . $station->{'state'} . "\n";
echo "Latitude: " . $station->{'lat'} . "\n";
echo "Longitude: " . $station->{'lon'} . "\n";
}
}
But currently it's not showing anything, i have searched for examples but i couldn't find any solution fot this problem.
Alternatively, you could use a simple foreach to iterate those values. Consider this example:
$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");
$parsed_json = json_decode($json_string, true); // <-- second parameter to TRUE to use it as an array
$desired_values = $parsed_json['location']['nearby_weather_stations']['pws']['station'];
foreach($desired_values as $key => $value) {
echo "<hr/>";
echo "City: " . $value['city'] . "<br/>";
echo "State: " . $value['state'] . "<br/>";
echo "Latitude: " . $value['lat'] . "<br/>";
echo "Longitude: " . $value['lon'] . "<br/>";
echo "<hr/>";
}
Sample Fiddle
How do you display the output of a 2 dimensional arrays
Here's the code:
I know how to display both of them separately doing.
foreach($Father as $value){
echo $value[0] . " " . $value[1] . "<br />";
}
foreach($Son as $value){
echo $value[0] . " " . $value[1] . "<br />";
}
try this - it assumes that each father has exactly 1 son though
$i = 0;
foreach($Father as $value){
echo $value[0] . " " . $value[1] . "<br />";
echo "-" . $Son[$i][0] . " " . $Son[$i][1] . "<br />";
$i += 1;
}
So I I've tried to make this code log an exception but it gives me the error message object of class domain_model could not be converted to string
The function looks as follows:
function errorLog($log, $error_type, $string, $file, $row, $error_hash, $error_trace)
{
$text = $error_hash . ' (' . date('Y-m-d H:i') . "):\n[Error type]: " . $error_type . "\n[Message]: " . $string . "\n[File]: " . $file . "\n[Row]: " . $row . "\n";
$text .= "[Trace]:\n";
foreach ($error_trace as $t)
{
$text .= ((isset($t['type']) && isset($t['object'])) ? $t['object'] . $t['type'] . $t['function'] : $t['function']) . "\n";
$text .= $t['file'] . "\n";
$text .= $t['line'] . "\n";
$text .= print_r($t['file'], 1) . "\n";
}
file_put_contents(BASE . $log . '.log', $text, FILE_APPEND);
}
After alot of thinking eventually saw that the line in making a mess is infact this one:
$text .= ((isset($t['type']) && isset($t['object'])) ? $t['object'] . $t['type'] . $t['function'] : $t['function']) . "\n";
And as I see it the only one in need of conversion should be $t['object'] however using (string)$t['object'] didn't work and still gives me the same error. Is there any other solution on how to convert it to a string than this?
I've looked at how they suggest it to be done here
I'm trying to create a variable that prints strings that are concatenated with results that are pulled from an array with a foreach loop, but every time I add on the actual array, I get a server error.
Here's my code:
$emailmsg .= "Price: " . $totalprice . " // Order: " .
foreach ($productarray as $book1=>$info1)
{if ($book1 > 0)
{echo $info1 . ", Quantity: " . $book1 . " // ";}
};
I've tried removing each semicolon as well as the echo statement to see if that was part of it but to no avail. Any help would be appreciated, thanks!
You have a dot at the end of the first line, I think the proper thing you want to do is:
$emailmsg = "Price: " . $totalprice . " // Order: ";
foreach ($productarray as $book1=>$info1)
{
if ($book1 > 0)
{
echo $emailmsg .= $info1 . ", Quantity: " . $book1 . " // ";
}
}
EDIT:
To have the message printed once with the order details:
$emailmsg = "Price: " . $totalprice . " // Order: ";
$order = '';
foreach ($productarray as $book1=>$info1)
{
if ($book1 > 0)
{
$order .= $info1 . ", Quantity: " . $book1 . " // ";
}
}
echo $emailmsg.$order;
Edit - I believe the 500 error is because of the stray dot .
just before the foreach loop
Not sure I'm following the input / desired output, but does something like this help?
foreach ($productarray as $book1=>$info1)
{if ($book1 > 0)
{$emailmsg .= "Price: " . $totalprice . " // Order: " . $info1 . ", Quantity: " . $book1 . " // ";}
};
If not, what does $productarray look like? and what does your desired output look like?