PHP 'foreach' array concatenate echo - php

The following will echo "12334".
However, I would like for the "12334" to be placed in a variable say $wordNumValue.
I know it might be a simple thing, but to me it is not. Any help is much appreciated.
<?php
$msg ="hello";
$arrEn = str_split($msg);
foreach ($arrEn as &$value) {
if ($value == 'h') {
echo "1";
} elseif ($value == 'e') {
echo "2";
} elseif ($value == 'l') {
echo "3";
} elseif ($value == 'o') {
echo "4";
} else {
echo 'NULL';
}
}
?>

<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = "";
foreach ($arrEn as &$value) {
if ($value == 'h') {
echo "1";
$wordNumValue .= "1";
} elseif ($value == 'e') {
echo "2";
$wordNumValue .= "2";
} elseif ($value == 'l') {
echo "3";
$wordNumValue .= "3";
} elseif ($value == 'o') {
echo "4";
$wordNumValue .= "4";
} else {
echo 'NULL';
}
}
?>

Try this:
<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = '';
foreach ($arrEn as &$value) {
if ($value == 'h') {
echo "1";
$wordNumValue .= "1";
} elseif ($value == 'e') {
echo "2";
$wordNumValue .= "2";
} elseif ($value == 'l') {
echo "3";
$wordNumValue .= "3";
} elseif ($value == 'o') {
echo "4";
$wordNumValue .= "4";
} else {
echo 'NULL';
}
}
But as you have many elseifs I, would do this instead:
<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = '';
foreach ($arrEn as &$value) {
switch ($value) {
case "h":
$wordNumValue .= "1";
break;
case "e":
$wordNumValue .= "2";
break;
case "l":
$wordNumValue .= "3";
break;
case "o":
$wordNumValue .= "4";
break;
default:
echo 'NULL';
}
}
echo $wordNumValue;
And finally I echoed $wordNumValue instead of echoing single numbers several times.

You can add each string onto a variable using the .= assignment operator;
<?php
$msg ="hello";
$arrEn = str_split($msg);
foreach ($arrEn as &$value) {
if ($value == 'h') {
$wordNumValue .= "1";
} elseif ($value == 'e') {
$wordNumValue .= "2";
} elseif ($value == 'l') {
$wordNumValue .= "3";
} elseif ($value == 'o') {
$wordNumValue .= "4";
}
}
?>
You don't need the final else. See this, PHP: Assignment Operators, and this, PHP: String Operators.
Hope this helps.

You don't need to change your code , before foreach add ob_start();
and after foreach add $wordNumValue = ob_get_clean();

Related

How to repeat php "if" 20 times?

I'm trying to repeat php "if" 20 times with dh_c_01_01. How I can do it?
if ($dh_c_01_01['id'] == $a_c_a_max) {
$dh_sq_01_01 = $a_b_s_05;
} elseif ($dh_c_01_01['id'] == $a_c_a_1_b) {
$dh_sq_01_01 = $a_b_s_06;
} elseif ($dh_c_01_01['id'] == $a_c_a_2_b) {
$dh_sq_01_01 = $a_b_s_07;
} elseif ($dh_c_01_01['id'] == $a_c_a_3_b) {
$dh_sq_01_01 = $a_b_s_08;
} elseif ($dh_c_01_01['id'] == $a_c_a_4_b) {
$dh_sq_01_01 = $a_b_s_09;
} elseif ($dh_c_01_01['id'] == $a_c_a_5_b) {
$dh_sq_01_01 = $a_b_s_10;
} elseif ($dh_c_01_01['id'] == $a_c_a_6_b) {
$dh_sq_01_01 = $a_b_s_11;
} elseif ($dh_c_01_01['id'] == $a_c_a_7_b) {
$dh_sq_01_01 = $a_b_s_12;
} elseif ($dh_c_01_01['id'] == $a_c_a_8_b) {
$dh_sq_01_01 = $a_b_s_13;
} elseif ($dh_c_01_01['id'] == $a_c_a_9_b) {
$dh_sq_01_01 = $a_b_s_14;
}
if ($dh_c_02_01['id'] == $a_c_a_max) {
$dh_sq_02_01 = $a_b_s_05;
} elseif ($dh_c_02_01['id'] == $a_c_a_1_b) {
$dh_sq_02_01 = $a_b_s_06;
} elseif ($dh_c_02_01['id'] == $a_c_a_2_b) {
$dh_sq_02_01 = $a_b_s_07;
} elseif ($dh_c_02_01['id'] == $a_c_a_3_b) {
$dh_sq_02_01 = $a_b_s_08;
} elseif ($dh_c_02_01['id'] == $a_c_a_4_b) {
$dh_sq_02_01 = $a_b_s_09;
} elseif ($dh_c_02_01['id'] == $a_c_a_5_b) {
$dh_sq_02_01 = $a_b_s_10;
} elseif ($dh_c_02_01['id'] == $a_c_a_6_b) {
$dh_sq_02_01 = $a_b_s_11;
} elseif ($dh_c_02_01['id'] == $a_c_a_7_b) {
$dh_sq_02_01 = $a_b_s_12;
} elseif ($dh_c_02_01['id'] == $a_c_a_8_b) {
$dh_sq_02_01 = $a_b_s_13;
} elseif ($dh_c_02_01['id'] == $a_c_a_9_b) {
$dh_sq_02_01 = $a_b_s_14;
}
[$dh_sq_03_01, ... , $dh_sq_20_01]
up to 20 times
I cannot be sure I understand your question correctly, or assume I know what your variables are or what their values are.
Based on your question, this example uses extra variables, for loops and dynamic variables. It also assumes that each of your dynamic variables exists:
<?php
$dh_c_limit = 20; // <-- loop limit
$a_c_a_limit = 9; // <-- loop limit
$a_b_s_addition = 5; // <-- based on your question
$a_c_a_max = 0; // <-- change this to whatever '$a_c_a_max' should be!
for ($dh_i = 1; $dh_i <= $dh_c_limit; $dh_i++) {
if(${"dh_c_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"}['id'] == $a_c_a_max){ // <-- checks against '$a_c_a_max'
${"dh_sq_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"} = ${"a_b_s_".str_pad($a_c_i,2,"0",STR_PAD_LEFT)}; // <-- set the '$dh_sq_' variable to '$a_b_s_' variable
}
else{
for ($a_c_i = 1; $a_c_i <= $a_c_a_limit; $a_c_i++) {
if(${"dh_c_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"}['id'] == ${"a_c_a_".$a_c_i."_b"}){ // <-- checks against '$a_c_a_' variable
${"dh_sq_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"} = ${"a_b_s_".str_pad(($a_c_i+$a_b_s_addition),2,"0",STR_PAD_LEFT)}; // <-- set the '$dh_sq_' variable to '$a_b_s_' variable
break; // <-- break the loop
}
}
}
}
?>
That is a bad habit. Try to use switch case, or use array as container
$array = [
'id1' => 'action1',
'id2' => 'action2',
...
];
$dh_sq_02_01 = $array[$dh_c_02_01['id']];
Just and example. Hope helps

PHP code won't work after adding html taggs

So I made PHP code that is used for signature, that is not problem right now the problem is that when I add <html></html> tags on the beginning of the code it won't show anything that I have done in PHP. So when I put html tags or make space in front of php there is nothing in page, but when I remove them everything work fine. I'm not expert in PHP so..
<?php
include_once("functions.php");
$sign = !isset($_GET['s']) ? 1 : $_GET['s'];
$uname = !isset($_GET["name"]) ? "None" : $_GET['name'];
// Connection & Website Settings
$ftpad = "";
$ftpuser = "";
$ftppass = "";
$comm = "SAMP";
$weburl = "www.incoming.com";
$usersdir = "/scriptfiles/Users";
//
$ftpcon = ftp_connect($ftpad,4112) or die("Error|I can't connect to the database: $ftpad, contact web master");
$login = ftp_login($ftpcon,$ftpuser,$ftppass);
$uname = "";
$name = $_GET['name'];
ftp_pasv($ftpcon,true);
$fhandle = fopen("tempsign_".hash('sha256',"$name").".tmp","w+");
ftp_fget($ftpcon,$fhandle,"$usersdir/$name.ini",FTP_ASCII);
$str = parse_ini_file2("tempsign_".hash('sha256',"$ime").".tmp");
fclose($fhandle);
$skin = $str['Skin'];
$rImg = ImageCreateFromPNG("./signs/1.png");
if (file_exists("./signs/skins/$skin.jpg"))
{
$skinImg = ImageCreateFromjpeg("./signs/skins/$skin.jpg");
}
else
{
$skinImg = ImageCreateFromjpeg("./signs/skins/0.jpg");
}
$cor_black = imagecolorallocate($rImg,0,0,0);
$cor_blue = imagecolorallocate($rImg,0,0,255);
$cor_lblue = imagecolorallocate($rImg,30,144,255);
$cor_green = imagecolorallocate($rImg,69,139,116);
$cor_red = imagecolorallocate($rImg,220,20,65);
$cor_wh = imagecolorallocate($rImg,255,255,255);
$cor_n = imagecolorallocate($rImg,246,74,14);
if(ftp_size($ftpcon,"$usersdir/$name.ini") == -1 || $ime == "None")
{
?>
<tr>
<center><td align="center" valign="top"><img src="logo.png" width="400" height="155" alt="logo" /></td></center>
</tr>
<style type="text/css">
body{
background-color:#000;
background-image:url(backg.png);
}
.
</style>
<?php
echo "<center><br/><br/><br/><br/><font color='#FF3333'>Error | That signature doesn't exist! Possible reasons:<br/><br/></font></center>";
echo "<center><font color='#0FB9FC'>1. You didn't enter your username in field for that<br/></font></center>";
echo "<center><font color='#0FB9FC'>2. You entered wrong username(Example Your_Name)<br/></font></center>";
echo "<center><font color='#0FB9FC'>3. Username that you entered isn't in our databse<br/></font></center>";
return 1;
}
else
{
ftp_pasv($ftpcon,true);
$fhandle = fopen("tempsign_".hash('sha256',"$name").".tmp","w+");
ftp_fget($ftpcon,$fhandle,"$usersdir/$name.ini",FTP_ASCII);
$str = parse_ini_file2("tempsign_".hash('sha256',"$name").".tmp");
fclose($fhandle);
imagettftext($rImg,12,0,11,39,$cor_n,"font.TTF",urldecode($name));
imagettftext($rImg,12,0,11,79,$cor_n,"font.TTF",urldecode($str['Level']));
imagettftext($rImg,12,0,173,120,$cor_n,"font.TTF",urldecode($str['Points']));
imagettftext($rImg,12,0,173,80,$cor_n,"font.TTF",urldecode($str['Hours']));
imagecopymerge($rImg, $skinImg, 308,6,0,0,80,100,100);
}
if($str['Sex'] == 0) { imagettftext($rImg,12,0,10,122,$cor_n,"font.TTF","Male"); }
else if($str['Sex'] == 1) { imagettftext($rImg,12,0,10,122,$cor_n,"font.TTF","Female"); }
if($str['GM'] > 0)
{
imagettftext($rImg, 13,0,5,161,$cor_green,"font.TTF","GameSupport");}
if($str['Admin'] > 0)
{
imagettftext($rImg, 13,0,5,161,$cor_red,"font.TTF","Admin");}
if($str['Leader'] > 0)
{
if($str['Leader'] == 1) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","LSPD"); }
else if($str['Leader'] == 2) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","FBI"); }
else if($str['Leader'] == 3) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","CNN"); }
else if($str['Leader'] == 4) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","La Cocaina"); }
else if($str['Leader'] == 5) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","La Cosa Nostra"); }
else if($str['Leader'] == 6) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Terror Squad Crew"); }
else if($str['Leader'] == 7) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","GSF"); }
else if($str['Leader'] == 8) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Yakuza"); }
else if($str['Leader'] == 9) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Russian Mafia"); }
else if($str['Leader'] == 10) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Underground Racers"); }
else if($str['Leader'] == 11) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Hitman Agency"); }
}
else if($str['Member'] > 0)
{
if($str['Member'] == 1) { imagettftext($rImg,13,0,171,40,$cor_wh,"font.TTF","LSPD"); }
else if($str['Member'] == 2) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","FBI"); }
else if($str['Member'] == 3) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","CNN"); }
else if($str['Member'] == 4) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","La Cocaine"); }
else if($str['Member'] == 5) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","La Cosa Nostra"); }
else if($str['Member'] == 6) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Terror Squad Crew"); }
else if($str['Member'] == 7) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","GSF"); }
else if($str['Member'] == 8) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Yakuza"); }
else if($str['Member'] == 9) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Russian Mafia"); }
else if($str['Member'] == 10) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Underground Racers"); }
else if($str['Member'] == 11) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Hitman Agency"); }
}
else
{
imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Civil");
}
ftp_close($ftpcon);
unset($str);
unset($ftpad);
unset($ftpuser);
unset($ftppass);
unset($ftpcon);
unset($login);
header('Content-type: image/png');
imagepng($rImg);
imagepng($skinImg);
?>
</style>
Please check your below code:
<?php
//Error reporting section
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include_once("functions.php");
$sign = !isset($_GET['s']) ? 1 : $_GET['s'];
$uname = !isset($_GET["name"]) ? "None" : $_GET['name'];
// Connection & Website Settings
$ftpad = "";
$ftpuser = "";
$ftppass = "";
$comm = "SAMP";
$weburl = "www.incoming.com";
$usersdir = "/scriptfiles/Users";
$ftpcon = ftp_connect($ftpad,4112) or die("Error|I can't connect to the database: $ftpad, contact web master");
$login = ftp_login($ftpcon,$ftpuser,$ftppass);
$uname = "";
$name = $_GET['name'];
ftp_pasv($ftpcon,true);
$fhandle = fopen("tempsign_".hash('sha256',"$name").".tmp","w+");
ftp_fget($ftpcon,$fhandle,"$usersdir/$name.ini",FTP_ASCII);
$str = parse_ini_file2("tempsign_".hash('sha256',"$ime").".tmp");
fclose($fhandle);
$skin = $str['Skin'];
$rImg = ImageCreateFromPNG("./signs/1.png");
if (file_exists("./signs/skins/$skin.jpg"))
{
$skinImg = ImageCreateFromjpeg("./signs/skins/$skin.jpg");
}
else
{
$skinImg = ImageCreateFromjpeg("./signs/skins/0.jpg");
}
$cor_black = imagecolorallocate($rImg,0,0,0);
$cor_blue = imagecolorallocate($rImg,0,0,255);
$cor_lblue = imagecolorallocate($rImg,30,144,255);
$cor_green = imagecolorallocate($rImg,69,139,116);
$cor_red = imagecolorallocate($rImg,220,20,65);
$cor_wh = imagecolorallocate($rImg,255,255,255);
$cor_n = imagecolorallocate($rImg,246,74,14);
if(ftp_size($ftpcon,"$usersdir/$name.ini") == -1 || $ime == "None")
{
?><tr>
<center><td align="center" valign="top"><img src="logo.png" width="400" height="155" alt="logo" /></td></center>
</tr>
<style type="text/css">
body{
background-color:#000;
background-image:url(backg.png);
}
</style><?php
echo "<center><br/><br/><br/><br/><font color='#FF3333'>Error | That signature doesn't exist! Possible reasons:<br/><br/></font></center>";
echo "<center><font color='#0FB9FC'>1. You didn't enter your username in field for that<br/></font></center>";
echo "<center><font color='#0FB9FC'>2. You entered wrong username(Example Your_Name)<br/></font></center>";
echo "<center><font color='#0FB9FC'>3. Username that you entered isn't in our databse<br/></font></center>";
return 1;
}
else
{
ftp_pasv($ftpcon,true);
$fhandle = fopen("tempsign_".hash('sha256',"$name").".tmp","w+");
ftp_fget($ftpcon,$fhandle,"$usersdir/$name.ini",FTP_ASCII);
$str = parse_ini_file2("tempsign_".hash('sha256',"$name").".tmp");
fclose($fhandle);
imagettftext($rImg,12,0,11,39,$cor_n,"font.TTF",urldecode($name));
imagettftext($rImg,12,0,11,79,$cor_n,"font.TTF",urldecode($str['Level']));
imagettftext($rImg,12,0,173,120,$cor_n,"font.TTF",urldecode($str['Points']));
imagettftext($rImg,12,0,173,80,$cor_n,"font.TTF",urldecode($str['Hours']));
imagecopymerge($rImg, $skinImg, 308,6,0,0,80,100,100);
}
if($str['Sex'] == 0) { imagettftext($rImg,12,0,10,122,$cor_n,"font.TTF","Male"); }
else if($str['Sex'] == 1) { imagettftext($rImg,12,0,10,122,$cor_n,"font.TTF","Female"); }
if($str['GM'] > 0){ imagettftext($rImg, 13,0,5,161,$cor_green,"font.TTF","GameSupport");}
if($str['Admin'] > 0){ imagettftext($rImg, 13,0,5,161,$cor_red,"font.TTF","Admin");}
if($str['Leader'] > 0){
if($str['Leader'] == 1) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","LSPD"); }
else if($str['Leader'] == 2) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","FBI"); }
else if($str['Leader'] == 3) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","CNN"); }
else if($str['Leader'] == 4) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","La Cocaina"); }
else if($str['Leader'] == 5) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","La Cosa Nostra"); }
else if($str['Leader'] == 6) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Terror Squad Crew"); }
else if($str['Leader'] == 7) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","GSF"); }
else if($str['Leader'] == 8) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Yakuza"); }
else if($str['Leader'] == 9) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Russian Mafia"); }
else if($str['Leader'] == 10) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Underground Racers"); }
else if($str['Leader'] == 11) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Hitman Agency"); }
}
else if($str['Member'] > 0)
{
if($str['Member'] == 1) { imagettftext($rImg,13,0,171,40,$cor_wh,"font.TTF","LSPD"); }
else if($str['Member'] == 2) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","FBI"); }
else if($str['Member'] == 3) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","CNN"); }
else if($str['Member'] == 4) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","La Cocaine"); }
else if($str['Member'] == 5) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","La Cosa Nostra"); }
else if($str['Member'] == 6) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Terror Squad Crew"); }
else if($str['Member'] == 7) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","GSF"); }
else if($str['Member'] == 8) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Yakuza"); }
else if($str['Member'] == 9) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Russian Mafia"); }
else if($str['Member'] == 10) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Underground Racers"); }
else if($str['Member'] == 11) { imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Hitman Agency"); }
}
else
{
imagettftext($rImg,13,0,171,40,$cor_n,"font.TTF","Civil");
}
ftp_close($ftpcon);
unset($str);
unset($ftpad);
unset($ftpuser);
unset($ftppass);
unset($ftpcon);
unset($login);
header('Content-type: image/png');
imagepng($rImg);
imagepng($skinImg);
?>

Images created with PHP are not displayed

I am working to a banner generator. All code works perfectly if I access : banner.php?id=<ID>. I'm working on localhost so, I know, if I want to see this image that I've created, I must write:
<img src="http://localhost/banner.php?id=<ID>" />
I can see image ONLY if I access direct link. Why I can't see it if I add it into <img> tag? I thought is because I use cURL to get some variables, but no.
<?php
error_reporting(E_ALL);
include("settings.php");
// GET CULOURS
if(!isset($_GET['color_title'])) $color_title = "FFC200"; else $color_title = substr($_GET['color_title'],0,6);
if(!isset($_GET['color_info'])) $color_info = "FFFFFF"; else $color_info = substr($_GET['color_info'],0,6);
if(!isset($_GET['bg'])) $bg = "bg1"; else $bg = $_GET['bg'];
if(!isset($_GET['rank'])) $rank = "0"; else $rank = (int)$_GET['rank'];
// Get Userid
if(!$_GET['id'] || !ctype_digit($_GET['id'])){
echo '<meta http-equiv="refresh" content="0;url=index.html">';
}
$steamid = $_GET['id'];
// Get username, avatar , kills, deaths and played time
$noDigit = 0;
$hoursPlayed = 0;
$weHaveCSGO = 0;
$nullJson = 0;
if(ctype_digit($steamid)){
$noDigit = 1;
$uuser = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$steamauth['apikey']."&steamids=".$steamid."");
$user = json_decode($uuser, true);
// Until here
if(isset($user['response']['players'][0]['steamid'])){
$nullJson = 1;
$customURL = $user['response']['players'][0]['profileurl'];
$username = $user['response']['players'][0]['personaname'];
$avatar = $user['response']['players'][0]['avatarfull'];
$privacy = $user['response']['players'][0]['communityvisibilitystate'];
if($privacy == 3){
$getGames = simplexml_load_file($customURL."games?tab=all&xml=1");
foreach($getGames->games->game as $game){
if($game->appID == "730"){
$weHaveCSGO = 1;
$hoursPlayed = $game->hoursOnRecord;
}
}
$xml = simplexml_load_file($customURL."?xml=1");
$customMessage = $xml->stateMessage;
if($weHaveCSGO == 1){
$ggame = file_get_contents("http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=".$steamauth['apikey']."&steamid=".$steamid."");
$game = json_decode($ggame);
foreach($game->playerstats->stats as $stats){
if($stats->name == "total_kills"){ $total_kills = $stats->value ; }
else if($stats->name == "total_deaths"){ $total_deaths = $stats->value ; }
else if($stats->name == "last_match_favweapon_id"){ $last_match_weapon = $stats->value ; }
else if($stats->name == "total_rounds_played"){ $total_rounds_played = $stats->value; }
else if($stats->name == "total_wins"){ $total_wins = $stats->value; }
}
}
}
// Convert weaponID to weaponName
if($last_match_weapon == "1") { $last_match_favweapon = "Desert Eagle"; }
else if($last_match_weapon == "2") { $last_match_favweapon = "Dual Berettas"; }
else if($last_match_weapon == "3") { $last_match_favweapon = "Five-Seven"; }
else if($last_match_weapon == "7") { $last_match_favweapon = "AK-47"; }
else if($last_match_weapon == "4") { $last_match_favweapon = "Glock-18"; }
else if($last_match_weapon == "8") { $last_match_favweapon = "AUG"; }
else if($last_match_weapon == "9") { $last_match_favweapon = "AWP"; }
else if($last_match_weapon == "10") { $last_match_favweapon = "Famas"; }
else if($last_match_weapon == "11") { $last_match_favweapon = "G3SG1"; }
else if($last_match_weapon == "13") { $last_match_favweapon = "Galil AR"; }
else if($last_match_weapon == "14") { $last_match_favweapon = "M249"; }
else if($last_match_weapon == "16") { $last_match_favweapon = "M4"; }
else if($last_match_weapon == "17") { $last_match_favweapon = "Mac-10"; }
else if($last_match_weapon == "19") { $last_match_favweapon = "P90"; }
else if($last_match_weapon == "24") { $last_match_favweapon = "UMP-45"; }
else if($last_match_weapon == "25") { $last_match_favweapon = "XM1014"; }
else if($last_match_weapon == "26") { $last_match_favweapon = "PP-Bizon"; }
else if($last_match_weapon == "27") { $last_match_favweapon = "Mag-7"; }
else if($last_match_weapon == "28") { $last_match_favweapon = "Negev"; }
else if($last_match_weapon == "29") { $last_match_favweapon = "SawedOff"; }
else if($last_match_weapon == "30") { $last_match_favweapon = "Tec-9"; }
else if($last_match_weapon == "31") { $last_match_favweapon = "Zeus x27"; }
else if($last_match_weapon == "32") { $last_match_favweapon = "P2000"; }
else if($last_match_weapon == "33") { $last_match_favweapon = "MP7"; }
else if($last_match_weapon == "34") { $last_match_favweapon = "MP9"; }
else if($last_match_weapon == "35") { $last_match_favweapon = "Negev"; }
else if($last_match_weapon == "36") { $last_match_favweapon = "P250"; }
else if($last_match_weapon == "38") { $last_match_favweapon = "Scar-20"; }
else if($last_match_weapon == "39") { $last_match_favweapon = "SG553"; }
else if($last_match_weapon == "40") { $last_match_favweapon = "SSG08"; }
else if($last_match_weapon == "42") { $last_match_favweapon = "Knife"; }
else if($last_match_weapon == "43") { $last_match_favweapon = "Flashbang"; }
else if($last_match_weapon == "44") { $last_match_favweapon = "HE Grenade"; }
else if($last_match_weapon == "45") { $last_match_favweapon = "Smoke Grenade"; }
else if($last_match_weapon == "46") { $last_match_favweapon = "Molotov"; }
else if($last_match_weapon == "47") { $last_match_favweapon = "Decoy Grenade"; }
else if($last_match_weapon == "48") { $last_match_favweapon = "Incendiary Grenade"; }
else if($last_match_weapon == "49") { $last_match_favweapon = "C4"; }
else if($last_match_weapon == "59") { $last_match_favweapon = "Knife"; }
else if($last_match_weapon == "60") { $last_match_favweapon = "M4"; }
else if($last_match_weapon == "61") { $last_match_favweapon = "USP-S"; }
else if($last_match_weapon == "63") { $last_match_favweapon = "CZ75-Auto"; }
else { $last_match_favweapon = "Unknown"; }
}
// Create rank image
switch($rank){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
$rank_image = "images/ranks/".$rank.".png";
break;
default:
$rank_image = "images/ranks/0.png";
break;
}
$rankimage = ImageCreateFromPNG($rank_image);
// Create banner
switch($bg){
case 'bg1':
case 'bg2':
case 'bg3':
case 'bg4':
$im = ImageCreateFromPNG("images/banners/$bg.png") or die ( 'GD Library not available atm.' );
break;
default:
$im = ImageCreateFromPNG("images/banners/bg1.png") or die ( 'GD Library not available atm.' );
break;
}
// Last match weaponID
$weapon = "images/weapons/hud/AK-47.png";
$lmweapon = ImageCreateFromPNG($weapon);
//Culours for text
$color_green = imagecolorallocate($im, 0, 183, 21);
$color_red = imagecolorallocate($im, 255, 0, 0);
$color_title = "0x$color_title";
$color_info = "0x$color_info";
$color_online = "0x24FF00";
$color_offline = "0xFF0000";
$color_black = "0x000000";
$color_bgreen = imagecolorallocate($im,0,160,0);
// Make some changes
$avatar = str_replace("https","http",$avatar);
// Get new sizes for avatar
list($width, $height) = getimagesize($avatar);
$newwidth = 90;
$newheight = 90;
// Load
$source = imagecreatefromjpeg($avatar);
// Lenght username
if(strlen($username) > 21){
$username = substr($username,0,21);
}
// Kills/Deaths Ratio
$kdr = #round($total_kills/$total_deaths,2);
// Win Ratio
$win = #round((($total_wins/$total_rounds_played)*100),2);
//Let's create image, but first check user
if($noDigit == 0 || $weHaveCSGO == 0 || $nullJson == 0){
imagettftext($im,20,0,140,60,$culoare_text_info,'fonts/arialbd.ttf',"This user doesn't exist !");
} else {
//Avatar
imagecopyresized($im, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Rank
imagecopy($im,$rankimage,96,5,0,0,imagesx($rankimage),imagesy($rankimage));
// Username
imagettftext($im,9,0,153,20,$color_info,'fonts/arialbd.ttf',$username);
// Status
$cauta = 'Last Online';
$cautare = strpos($customMessage, $cauta);
$cauta2 = 'In-Game<br/>Counter-Strike: Global Offensive';
$cautare2 = strpos($customMessage, $cauta2);
$cauta3 = 'In-Game<br/>';
$cautare3 = strpos($customMessage, $cauta3);
if($customMessage == "Online"){
imagettftext($im,9,90,347,63,$color_bgreen,'fonts/arialbd.ttf',"Online");
}elseif ($cautare !== false) {
imagettftext($im,7,0,290,20,$color_red,'fonts/arialbd.ttf',"Offline");
} elseif($cautare2 !== false){
imagettftext($im,7,0,282,20,$color_bgreen,'fonts/arialbd.ttf',"Playing CS:GO");
} elseif($cautare3 !== false){
imagettftext($im,7,0,276,20,$color_title,'fonts/arialbd.ttf',"IN OTHER GAME");
}
// Fav weapons from last match
imagecopy($im,$lmweapon,107,30,0,0,imagesx($lmweapon),imagesy($lmweapon));
imagettftext($im,9,0,110,70,$color_title,'fonts/arialbd.ttf',$last_match_favweapon);
// K/D Ratio
imagettftext($im,11,0,175,52,$color_info,'fonts/arialbd.ttf',$kdr);
imagettftext($im,9,0,177,70,$color_title,'fonts/arialbd.ttf',"K/D");
// Win RATIO
imagettftext($im,11,0,220,52,$color_info,'fonts/arialbd.ttf', $win."%");
imagettftext($im,9,0,222,70,$color_title,'fonts/arialbd.ttf',"WIN %");
// Hours Played
imagettftext($im,11,0,280,52,$color_info,'fonts/arialbd.ttf',$hoursPlayed."h");
imagettftext($im,9,0,282,70,$color_title,'fonts/arialbd.ttf',"TIME");
}
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
This section is wrong
<img src="http://localhost/banner.php?id=<ID>" />
It should be
<img src="http://localhost/path to your image" />
If you have done it for, onclick go to some page,
then it should be
<a href="http://localhost/banner.php?id=<ID>">
<img src="http://localhost/path to your image" />
</a>
Can you show us your code? Which graphics library do you use? I had no problems using GD2 on my local computer.
E.g.
<?php
header('Content-type: image/png');
$img = imagecreate(256, 256);
$background = imagecolorallocate($img, 255, 255, 0);
$color = imagecolorallocate($img, $_GET['red'], $_GET['blue'], $_GET['green']);
imagearc($img, 128, 128, 64, 64, 0, 0, $color);
imagepng($img);
?>
And in the HTML file then:
<img src="img.php?red=255&blue=0&green=0" />
Worked for me...
When i had changed file name "banner.php" to other name, ex: 'counter.php', ...
The same code is working fine. I don't know what's cause of file name with name = "banner" produced this error

How to condense multiple IF statements simply in PHP

Here if my code. It works and runs and does everything I want it to do, but its too long. How can I condense it? My PHP level is basic.
Would I use an array? or a function? Can you kindly post the code you would use to make this more efficient?
<?php
$product = $result[CategoryId];
if ($product == "1") {
echo "Tonneau Cover to suit";
} elseif ($product == "2") {
echo "Clip On Tonneau Cover to suit";
} elseif ($product == "3") {
echo "INSERT";
} elseif ($product == "4") {
echo "INSERT";
} elseif ($product == "5") {
echo "INSERT";
} elseif ($product == "6") {
echo "INSERT";
} elseif ($product == "7") {
echo "INSERT";
} elseif ($product == "8") {
echo "INSERT";
} elseif ($product == "9") {
echo "INSERT";
} elseif ($product == "10") {
echo "INSERT";
} elseif ($product == "11") {
echo "INSERT";
} elseif ($product == "12") {
echo "INSERT";
} elseif ($product == "13") {
echo "INSERT";
} elseif ($product == "14") {
echo "INSERT";
} elseif ($product == "15") {
echo "INSERT";
} elseif ($product == "16") {
echo "INSERT";
} elseif ($product == "17") {
echo "INSERT";
} else {
echo ".";
}
?>
yes you can use array in this manner
$product = $result[CategoryId];
$ogh=["Tonneau Cover to suit","Clip On Tonneau Cover to suit","INSERT"];
if(count($ogh)>$product)
echo $ogh[$product];
else
echo "Invalid";
Based on what you have in the example:
<?php
$product = (int) $result['CategoryId'];
if ($product === 1) {
echo "Tonneau Cover to suit";
} elseif ($product === 2) {
echo "Clip On Tonneau Cover to suit";
} elseif ($product >=3 || $product <=17) {
echo "INSERT";
} else {
echo ".";
}
?>

Display files with syntax highlighting using PHP

I am working on a web based application using php (not using Java), in which I am required to display source code files (java,c,c++,Python etc..) with syntax highlighting on the web page. I am clueless on as to how to display the source code files. Any help will be appreciated.
One of the options is use a existing syntax highlighter like a google one.
Setting it up is very simple. All you have to do for basic usage is include the code in your html page in <pre> sections and apply a class attribute that is programming language.
<pre name="code" class="c-sharp">
class Foo
{
}
</pre>
16 Free Javascript Code Syntax Highlighters For Better Programming has a very exhaustive list of lot of options. repeated here in case the site ever goes down
SyntaxHighlighter
GeSHi - Generic Syntax Highlighter
quickhighlighter
google-code-prettify
pygments.
HIGHLIGHT.JS
Lighter.js – Syntax Highlighter written in MooTools
SHJS – Syntax Highlighting in JavaScript
CodePress – Online Real Time Syntax Highlighting Editor
Chili jQuery code highlighter plugin
Highlight – Code & Syntax highlighting by Andre Simon
BeautyOfCode: jQuery Plugin for Syntax Highlighting
JUSH – JavaScript Syntax Highlighter
Ultraviolet – Syntax Highlighting Engine
DlHighlight – JavaScript Syntax Highlighting Engine
Syntax highlighter for JavaScript
Syntax highlighter is used to show the source code program is colorful, so the reader can easily read/understand your code after integration. In this program, I added various elements (reserve words, parenthesis, comment, and quotes etc..) for highlighting. You can add/modify this code based on your Web Application/Programming Blog.
This syntax highlighter code is independent to any language, so you add integration with any programming language.
Please find the source code from my tech blog - http://www.algonuts.info/how-to-develop-a-source-code-syntax-highlighter-using-php.html
<?php
include_once("keywords.php");
class highlighter {
private $fileName;
private $fileNameColor;
private $fileExtension;
private $parenthesisColor;
private $insideParenthesisColor;
private $keywordColor;
private $backGroundColor;
private $borderColor;
private $leftBorderColor;
private $quotesColor;
private $commentColor;
public function __construct() {
$this->fileName = "";
$this->fileExtension = "";
//Color Configuration
$this->fileNameColor = "#286090";
$this->keywordColor = "green";
$this->backGroundColor = "#fdfefe";
$this->borderColor = "#e3e3e3";
$this->leftBorderColor = "#605a56";
$this->parenthesisColor = "#ec7700";
$this->insideParenthesisColor = "#ec7700";
$this->bracketColor = "#ec7700";
$this->insideBracketColor = "#ec7700";
$this->quotesColor = "#6a2c70";
$this->commentColor = "#b8b0b0";
}
public function applycolor($fileLocation = "") {
if($fileLocation == "")
{ return; }
else
{
if(file_exists($fileLocation)) {
$temp = explode("/",$fileLocation);
$this->fileName = trim(end($temp));
$temp = explode(".",$this->fileName);
$this->fileExtension = trim(end($temp));
$fileContent = trim(file_get_contents($fileLocation, true));
$fileContent = htmlentities($fileContent,ENT_NOQUOTES);
if($fileContent == "")
{ return; }
}
else
{ return; }
}
$line = 1;
$outputContent = "<div class=\"divblock\"><b>".$line."</b> ";
$characterBuffer = "";
$blockFound = 0;
$blockFoundColor = array();
$parenthesisFound = 0;
$bracketFound = 0;
$counter = 0;
$lastCharacter = "";
$contentSize = strlen($fileContent);
while($counter < $contentSize) {
$character = $fileContent[$counter];
$code = intval(ord($character));
if($blockFound == 0 && (($code >= 97 && $code <= 122) || ($code >= 65 && $code <= 90))) //Fnd alphabetic characters
{ $characterBuffer .= $character; }
else
{
if($code == 10) { //Find EOL (End of Line)
if($this->checker($characterBuffer))
{ $characterBuffer = "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
$line++;
if($blockFound == 0)
{ $outputContent .= $characterBuffer."</div>".$character."<div class=\"divblock\"><b>".$line."</b> "; }
else
{ $outputContent .= $characterBuffer."</font></div>".$character."<div class=\"divblock\"><b>".$line."</b> <font color='".$blockFoundColor[$blockFound-1]."'>"; }
$characterBuffer = "";
}
else if($code == 32) { //Find Space
if($characterBuffer != "") {
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>".$character; }
else
{ $outputContent .= $characterBuffer.$character; }
$characterBuffer = "";
}
else
{ $outputContent .= $character; }
}
else if($character == "\"" || $character == "'") { //Find Quotes
if($characterBuffer != "")
{
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
else
{ $outputContent .= $characterBuffer; }
$characterBuffer = "";
}
$outputContent .= "<font color='".$this->quotesColor."'>".$character;
$foundCharacter = $character;
$counter++;
while($counter < $contentSize) {
$character = $fileContent[$counter];
if($character == $foundCharacter) {
$outputContent .= $character;
if($lastCharacter == "\\") {
$lastCharacter = "";
}
else
{ break; }
}
else if($character == "\\" && $lastCharacter == "\\") {
$outputContent .= $character;
$lastCharacter = "";
}
else
{
$lastCharacter = $character;
$code = intval(ord($character));
if($code != 10)
{ $outputContent .= $character; }
else
{
$line++;
$outputContent .= "</font></div>".$character."<div class=\"divblock\"><b>".$line."</b> <font color='".$this->quotesColor."'>";
}
}
$counter++;
}
$outputContent .= "</font>";
}
else if($character == "(" || $character == ")") { //Find Parenthesis
if($characterBuffer != "")
{
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
else
{ $outputContent .= $characterBuffer; }
$characterBuffer = "";
}
if($parenthesisFound == 0) {
$blockFoundColor[$blockFound] = $this->insideParenthesisColor;
$outputContent .= "<font color='".$this->parenthesisColor."'>".$character."</font><font color='".$this->insideParenthesisColor."'>";
$parenthesisFound++;
$blockFound++;
}
else
{
if($character == "(") {
$parenthesisFound++;
}
if($character == ")") {
$parenthesisFound--;
}
if($parenthesisFound == 0) {
$outputContent .= "</font><font color='".$this->parenthesisColor."'>".$character."</font>";
$blockFound--;
unset($blockFoundColor[$blockFound]);
}
else
{ $outputContent .= $character; }
}
}
else if($character == "[" || $character == "]") { //Find Bracket
if($characterBuffer != "")
{
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
else
{ $outputContent .= $characterBuffer; }
$characterBuffer = "";
}
if($bracketFound == 0) {
$blockFoundColor[$blockFound] = $this->insideBracketColor;
$outputContent .= "<font color='".$this->bracketColor."'>".$character."</font><font color='".$this->insideBracketColor."'>";
$bracketFound++;
$blockFound++;
}
else
{
if($character == "[") {
$bracketFound++;
}
if($character == "]") {
$bracketFound--;
}
if($bracketFound == 0) {
$outputContent .= "</font><font color='".$this->bracketColor."'>".$character."</font>";
$blockFound--;
unset($blockFoundColor[$blockFound]);
}
else
{ $outputContent .= $character; }
}
}
else if($character == "/" && (isset($fileContent[$counter+1]) && ($fileContent[$counter+1] == "*" || $fileContent[$counter+1] == "/"))) { //Find Comment
if($characterBuffer != "")
{
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
else
{ $outputContent .= $characterBuffer; }
$characterBuffer = "";
}
$blockFound++;
$outputContent .= "<font color='".$this->commentColor."'>".$fileContent[$counter].$fileContent[$counter+1];
if($fileContent[$counter+1] == "*") {
$counter += 2;
$checkCharacter = "*";
while($counter < $contentSize) {
$outputContent .= $fileContent[$counter];
if($fileContent[$counter] == $checkCharacter) {
if($checkCharacter == "*")
{ $checkCharacter = "/"; }
else
{
$blockFound--;
$outputContent .= "</font>";
break;
}
}
$counter++;
}
}
else
{
$counter += 2;
while($counter < $contentSize) {
$character = $fileContent[$counter];
$code = intval(ord($character));
if($code == 10) {
$counter--;
$blockFound--;
$outputContent .= "</font>";
break;
}
$outputContent .= $character;
$counter++;
}
}
}
else if($characterBuffer != "")
{
if($this->checker($characterBuffer))
{ $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>".$character; }
else
{ $outputContent .= $characterBuffer.$character; }
$characterBuffer = "";
}
else
{ $outputContent .= $character; }
}
$counter++;
}
$outputContent .= "</div>";
$rerurnData = "<div class='filenamestyle' style='color:".$this->fileNameColor.";'>".$this->fileName."</div>"; //Show filename
$rerurnData .= "<div><pre><div class='codebox' style='background-color:".$this->backGroundColor.";border: 1px solid ".$this->borderColor.";border-left: 4px solid ".$this->leftBorderColor.";'>".$outputContent."</div></pre></div>";
return $rerurnData;
}
private function checker($value) {
global $languageKeywords;
if(isset($languageKeywords[$this->fileExtension])) {
$value = trim($value);
if(in_array($value,$languageKeywords[$this->fileExtension]))
{ return 1; }
else
{ return 0; }
}
}
}
?>

Categories