HTML & PHP Information Table - php

So i'm hosting servers and trying to make a table displaying the information server name players etc i have a script that displays this but i suck at css, What i'm trying to get is a little like in this website http://echelon.kthx.at/pubbans.php
<?php
include ("BC2Conn.php");
// opens a connection to gameserver
$BC2Conn = new BC2Conn("31.185.143.136", 48888);
if (!$BC2Conn->isConnected()) {
echo "Connection could not be established. " .
"To debug, set '-d' as 3rd parameter to new connection.<br />" .
"<br />" .
"Example: \$BC2Conn = new BC2Conn(\"127.0.0.1\", 48888, \"-d\");";
return 0; // stop executing this script
}
// secure login
// $BC2Conn->loginSecure("password");
// unsecure login (not salted)
// some random serverinformation
echo "Servername: " . $BC2Conn->getServerName() . "<br />";
echo "Players: " . $BC2Conn->getCurrentPlayers() . "/" . $BC2Conn->getMaxPlayers() . "<br />";
echo "Playmode: " . $BC2Conn->getCurrentPlaymodeName() . "<br />";
echo "Current Map: " . $BC2Conn->getCurrentMapName() . "<br /><br /><br /><u>Players:</u><br /><br />";
// playerlist
$playerNames = $BC2Conn->getPlayerlistNames();
foreach ($playerNames as $key => $content) {
if ($BC2Conn->getPlayerClantag($content) != "") {
echo "[" . $BC2Conn->getPlayerClantag($content) . "]";
}
echo " " . $BC2Conn->getPlayername($content) . " - Kills: ";
echo $BC2Conn->getPlayerKills($content) . " | Deaths: ";
echo $BC2Conn->getPlayerDeaths($content) . " | Score: ";
echo $BC2Conn->getPlayerScore($content) . "<br />";
}
// logout
$BC2Conn->logout();
?>

Related

How to list all value in array one by one for many items from json URL

<?php
$id = '2422414574';
$json = file_get_contents("http://xxxxxx.com/api.php?token=xxxx&id=xxxx");
$data = json_decode($json);
echo $data->phim[0]->filmName . "<br/>";
echo $data->phim[0]->epsList[0]->name . " - ";
echo $data->phim[0]->epsList[0]->id . "<br/>";
echo $data->phim[0]->epsList[1]->name . " - ";
echo $data->phim[0]->epsList[1]->id . "<br/>";
echo $data->phim[0]->epsList[2]->name . " - ";
echo $data->phim[0]->epsList[2]->id . "<br/>";
echo $data->phim[0]->epsList[3]->name . " - ";
echo $data->phim[0]->epsList[3]->id . "<br/>";
echo $data->phim[0]->epsList[4]->name . " - ";
echo $data->phim[0]->epsList[4]->id . "<br/>";
echo $data->phim[0]->epsList[5]->name . " - ";
echo $data->phim[0]->epsList[5]->id . "<br/>";
echo $data->phim[0]->epsList[6]->name . " - ";
echo $data->phim[0]->epsList[6]->id . "<br/>";
echo $data->phim[0]->epsList[7]->name . " - ";
echo $data->phim[0]->epsList[7]->id . "<br/>";
echo $data->phim[0]->epsList[xxxx]->name . " - ";
echo $data->phim[0]->epsList[xxxx]->id . "<br/>";
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
As you can see, I have to repeat 1 code in many time. Is there any short way to do this without repeat code?
For example, I want to get all values from id and name from this URL, which have 24 items. That means I have to repeat code in 24 times :(
what about foreach()?
foreach($data->phim as $phim)
{
echo $phim->filmName . "<br/>";
foreach($phim->epsList as $epsList)
{
echo $epsList->name . " - ";
echo $epsList->id . "<br/>";
}
}
You need to iterate over it using a foreach loop. This is one of the basic functions of PHP and there are many tutorials for it.

PHP: Return multiple results by if else statement

What I want to achieve is to check multiple domain availability.
Here is my code:
<?php
//$domain = 'example.com';
$domain = array('example.com', 'foo.com');
if ( checkdnsrr(in_array($domain), 'ANY') ) {
$echo_checkdnsrr = in_array($domain) . " DNS Record found (checkdnsrr)" . "<br />" . PHP_EOL;
}
else {
$echo_checkdnsrr = in_array($domain) . " NO DNS Record found (checkdnsrr)" . "<br />" . PHP_EOL;
}
if ( gethostbyname(in_array($domain)) != in_array($domain) ) {
$echo_gethostbyname = in_array($domain) . " DNS Record found (gethostbyname)" . "<br />" . PHP_EOL;
}
else {
$echo_gethostbyname = in_array($domain) . " NO DNS Record found (gethostbyname)" . "<br />" . PHP_EOL;
}
$separator = "-------------------------------------------" . "<br />" . PHP_EOL;
print_r($echo_checkdnsrr);
print_r($echo_gethostbyname);
echo $separator;
?>
What I want to achieve:
example.com DNS Record found (checkdnsrr)
example.com DNS Record found (gethostbyname)
-------------------------------------------
foo.com DNS Record found (checkdnsrr)
foo.com DNS Record found (gethostbyname)
-------------------------------------------
Thank You for your help!
Thank You to #fluinc, here is my final code:
$domains = array('example.com', 'foo.com');
foreach ($domains as $domain) {
if (checkdnsrr($domain, 'ANY') || gethostbyname($domain) != $domain) {
echo "TAKEN " . $domain . "<br />" . PHP_EOL;
} else {
echo "AVAILABLE " . $domain . "<br />" . PHP_EOL;
}
}
This is what it's returns:
TAKEN example.com
TAKEN foo.com
Later on it's super easy to import in excel, choose space as separator and find out the available domains with a query.
Use a foreach to loop through the domains, Your use of in_array is wrong and not needed.
$domains = array('example.com', 'foo.com');
foreach ($domains as $domain) {
if (checkdnsrr($domain, 'ANY')) {
echo $domain . " DNS Record found (checkdnsrr)<br />". PHP_EOL;
} else {
echo $domain . " NO DNS Record found (checkdnsrr)<br />". PHP_EOL;
}
if (gethostbyname($domain) != $domain) {
echo $domain . " DNS Record found (gethostbyname)<br />". PHP_EOL;
} else {
echo $domain . " NO DNS Record found (gethostbyname)<br />". PHP_EOL;
}
echo "-------------------------------------------<br />". PHP_EOL;
}
Updated as per comment
$domains = array('example.com', 'foo.com');
foreach ($domains as $domain) {
if (checkdnsrr($domain, 'ANY') || gethostbyname($domain) != $domain) {
echo $domain . " TAKEN<br />". PHP_EOL;
} else {
echo $domain . " AVAILABLE<br />". PHP_EOL;
}
echo "-------------------------------------------<br />". PHP_EOL;
}
Try using foreach() and echo out each result as it is processed:
$domains = array('example.com', 'foo.com');
foreach($domains as $domain){
if(checkdnsrr(in_array($domain), "ANY")){
echo in_array($domain) . " DNS Record found (checkdnsrr)" . "<br />" . PHP_EOL;
} else {
echo in_array($domain) . " NO DNS Record found (checkdnsrr)" . "<br />" . PHP_EOL;
}
if(gethostbyname(in_array($domain)) != in_array($domain)) {
echo in_array($domain) . " DNS Record found (gethostbyname)" . "<br />" . PHP_EOL;
} else {
echo in_array($domain) . " NO DNS Record found (gethostbyname)" . "<br />" . PHP_EOL;
}
echo "-------------------------------------------" . "<br />" . PHP_EOL;
}
Shorter:
$domains = array('example.com', 'foo.com');
foreach($domains as $domain){
echo in_array($domain).(checkdnsrr(in_array($domain), "ANY") ? "NO " : "")."DNS Record found (checkdnsrr)" . "<br />" . PHP_EOL;
echo in_array($domain).(gethostbyname(in_array($domain))!= in_array($domain) ? "NO " : "")."Record found (gethostbyname)" . "<br />" . PHP_EOL;
echo "-------------------------------------------" . "<br />" . PHP_EOL;
}

If statements reading entries from database won't show

the below code contains if statements depending on usertype and status. It won't display anything. In the code above i had usertype==1 and status==1 and it is working as it should but this below code won't work. Any suggestions? Please ignore the code i have as i am a beginner and the code is messy.
} else if ($usertype == 2) {
$server1 = "localhost";
$user1 = "r";
$pass1 = "";
$db1 = "";
$db4 = "";
$user5 = $_SESSION['username'];
$mysqli = new Mysqli($server1, $user1, $pass1, $db1) or mysqli_error($mysqli);
$mysqli6 = new Mysqli($server1, $user1, $pass1, $db4) or mysqli_error($mysqli);
$name2= $mysqli6->query("SELECT name FROM head WHERE username= '$user5'")->fetch_object()->name2;
$status2 = $mysqli->query("SELECT status FROM Overrides WHERE head = '$name2'")->fetch_object()->status2;
$headforms = $mysqli->query("SELECT * FROM Overrides WHERE head = '$name2' ");
$num_rows2 = mysqli_num_rows($headforms);
if ($status2 == 2) {
echo "Overrides today: " . $num_rows2;
while($row2 = mysqli_fetch_array($headforms)) {
echo "<br /><br />First Name: " . $row2['name'] . "<br />";
echo "<br />Middle Name: " . $row2['mname'] . "<br />";
echo "<br />Family Name: " . $row2['fname'] . "<br />";
echo "<br />Student ID: " . $row2['sid'] . "<br />";
echo "<br />Scolarship: " . $row2['sc'] . "<br />";
echo "<br />Phone No: " . $row2['phone'] . "<br />";
echo "<br />Email: " . $row2['email'] . "<br />";
echo "<br />Class: " . $row2['class'] . "<br />";
echo "<br />Section: " . $row2['section'] . "<br />";
echo "<br />Semester: " . $row2['semester'] . "<br />";
}
}
?>
Concerning the database entries and data, i have checked them and i'm 100% sure it should display correct info.
I did a test : i tried doing print_r($status2); and print_r($name2); but page is empty, could the problem be with the userype == 2 since it's displaying nothing.
errors:
Notice: Undefined property: stdClass::$name2 in /home/aukwizcq/public_html/override.php on line 310
Notice: Trying to get property of non-object in /home/aukwizcq/public_html/override.php on line 311

Issues with MySQL query - INSERT INTO

So, I'll post the code below. Beneath the code is where I will pose my question.
if (!empty($_SESSION['username']) && !empty($_SESSION['password']))
{
$server=mysql_real_escape_string($_POST['server']);
$teamname=mysql_real_escape_string($_POST['teamname']);
$creator=$_SESSION['username'];
$verify = mysql_real_escape_string($_POST['verify']);
$date = date("F j, Y, g:i a");
if (!empty($teamname)) {
// if ($verify == "wookie" ||
// $verify == "Wookie" ||
// $verify == "WOOKIE")
// {
$sql="INSERT INTO rated_teams (server, name, creator, created, players, win, loss)
VALUES ('$server', '$teamname', '$creator','$date', '', '', '')";
if (mysql_query($sql,$con))
{
echo "<p>Added ". $teamname . " on " . $server . " by " . $creator . " on " . $date ." <br /><a href='myprofile.php'>Return to Profile</a></p>";
}
else
{
echo $sql . "<br />";
echo "<br /><h1>Error</h1>";
echo "<p><a href='myprofile.php'>Sorry, your team registration has failed. Please go back and try again.</a></p>
<br />" . $teamname . " on " . $server . " by " . $creator . " on " . $date;
}
//} else { echo "That isn't how you spell Wookie!"; }
} else { echo "Team Name is empty, <a href='myprofile.php'>go back and give yourself a Team Name</a>"; }
} else { echo "You must be <a href='login.php'>logged in</a>!"; }
This issue is that the line "if (mysql_query($sql,$con))" goes directly to the ELSE. I'm assuming the problem lies with my $sql but I can't pinpoint where it is. Another pair of eyes would really help. Thanks a bunch!
To trace errors with mysql_query() , you should use mysql_error(). Here's an example, inspired of one of the PHP mysql_error() doc
$sql="INSERT INTO rated_teams (server, name, creator, created, players, win, loss)
VALUES ('$server', '$teamname', '$creator','$date', '', '', '')";
mysql_query($sql,$con);
if (mysql_errno()) {
$error = "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$sql\n<br>";
// Your stuff
echo $sql . "<br />";
echo "<br /><h1>Error</h1>";
echo "<p><a href='myprofile.php'>Sorry, your team registration has failed. Please go back and try again.</a></p>
<br />" . $teamname . " on " . $server . " by " . $creator . " on " . $date;
}
else {
echo "<p>Added ". $teamname . " on " . $server . " by " . $creator . " on " . $date ." <br /><a href='myprofile.php'>Return to Profile</a></p>";
}
Also, you should use PDO or mysqli, since mysql_* are deprecated since PHP 5.x

File uploads empty string

Uploading an image into a directory, it seems to always try upload "" which is nothing. Rather then an actual image. What am I doing below. I know the value is "" because of what it inserts into the database.
<li class="formProductImage">
<span>Image:</span>
<input type="file" name="productImage" />
</li>
and the php:
if ($_FILES["productImage"]["error"] > 0)
{
echo "Return Code: " . $_FILES["productImage"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["productImage"]["name"] . "<br />";
echo "Type: " . $_FILES["productImage"]["type"] . "<br />";
echo "Size: " . ($_FILES["productImage"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["productImage"]["tmp_name"] . "<br />";
$filename = mysql_real_escape_string($_FILES['productImage']['name']);
$query = "UPDATE products SET image = '$filename' WHERE name = '$name'";
$result = mysql_query($query);
if (mysql_affected_rows() == 1) {
// Show thank you message
echo '<span style="color:green;">Your image was added to database correctly.</span>';
if (file_exists("uploaded/" . $_FILES["productImage"]["name"]))
{
echo $_FILES["productImage"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["productImage"]["tmp_name"],
"uploaded/" . $_FILES["productImage"]["name"]);
echo "Stored in: " . "uploaded/" . $_FILES["productImage"]["name"];
}
} else {
echo '<font color="red">Image note inserted into database.</font>';
echo mysql_error();
}
}
}
}
Make sure you have enctype="multipart/form-data" in the <form> tag.
change this line:
$filename = mysql_real_escape_string($_FILES['file']['name']);
to :
$filename = mysql_real_escape_string($_FILES['productImage']['name']);

Categories