I need to create a text file from a foreach loop (code below) but i'm not sure how. So far I have tried to use fwrite but I'm not to sure how to write that much data to a txt file. I also tried to use file_put_content(), it worked but it displayed all of the HTML and had no line breaks. Any ideas on how to do display it?
Foreach loop
echo "<p>" . 'custom_weapons_v3' . "</p>";
echo "<p>" . '{' . "</p>";
foreach($arr as $key => $a){
echo "<br>";
$check = $a['steamid'];
echo $check;
echo "<br>";
echo "{";
echo "<br>";
foreach($data as $key => $r){
$check2 = $r['steam'];
if($check == $check2){
echo " \"{$r['wepid']}\" //{$r['wepname']}<br />";
echo '{' . "<br />";
echo '"level"' . ' "' . $r['weplvl'] . '"' . "<br />";
echo '"quality"' . ' ' . '"' . $r['weprare'] . '"' . "<br />";
if($r['attr1'] === 0 || $r['attr1'] === ''){
echo '';
}else{
echo '"1"' . ' "' . $r['attr1'] . ' ; ' . $r['val1'] . '"' . "<br />";
}
if($r['attr2'] === 0 || $r['attr2'] === ''){
echo '';
}else{
echo '"2"' . ' "' . $r['attr2'] . ' ; ' . $r['val2'] . '"' . "<br />";
}
if($r['attr3'] === 0 || $r['attr3'] === ''){
echo '';
}else{
echo '"3"' . ' "' . $r['attr3'] . ' ; ' . $r['val4'] . '"' . "<br />";
}
if($r['attr4'] === 0 || $r['attr4'] === ''){
echo '';
}else{
echo '"4"' . ' "' . $r['attr4'] . ' ; ' . $r['val4'] . '"' . "<br />";
}
if($r['attr5'] === 0 || $r['attr5'] === ''){
echo '';
}else{
echo '"5"' . ' "' . $r['attr5'] . ' ; ' . $r['val5'] . '"' . "<br />";
}
if($r['attr6'] === 0 || $r['attr6'] === ''){
echo '';
}else{
echo '"6"' . ' "' . $r['attr6'] . ' ; ' . $r['val6'] . '"' . "<br />";
}
if($r['attr7'] === 0 || $r['attr7'] === ''){
echo '';
}else{
echo '"7"' . ' "' . $r['attr7'] . ' ; ' . $r['val7'] . '"' . "<br />";
}
if($r['attr8'] === 0 || $r['attr8'] === ''){
echo '';
}else{
echo '"8"' . ' "' . $r['attr8'] . ' ; ' . $r['val8'] . '"' . "<br />";
}
if($r['attr9'] === 0 || $r['attr9'] === ''){
echo '';
}else{
echo '"9"' . ' "' . $r['attr9'] . ' ; ' . $r['val9'] . '"' . "<br />";
}
if($r['attr10'] === 0 || $r['attr10'] === ''){
echo '';
}else{
echo '"10"' . ' "' . $r['attr10'] . ' ; ' . $r['val10'] . '"' . "<br />";
}
if($r['attr11'] === 0 || $r['attr11'] === ''){
echo '';
}else{
echo '"11"' . ' "' . $r['attr11'] . ' ; ' . $r['val11'] . '"' . "<br />";
}
if($r['attr12'] === 0 || $r['attr12'] === ''){
echo '';
}else{
echo '"12"' . ' "' . $r['attr12'] . ' ; ' . $r['val12'] . '"' . "<br />";
}
if($r['attr13'] === 0 || $r['attr13'] === ''){
echo '';
}else{
echo '"13"' . ' "' . $r['attr13'] . ' ; ' . $r['val13'] . '"' . "<br />";
}
if($r['attr14'] === 0 || $r['attr14'] === ''){
echo '';
}else{
echo '"14"' . ' "' . $r['attr14'] . ' ; ' . $r['val14'] . '"' . "<br />";
}
if($r['attr15'] === 0 || $r['attr15'] === ''){
echo '';
}else{
echo '"15"' . ' "' . $r['attr15'] . ' ; ' . $r['val15'] . '"' . "<br />";
}
if($r['attr16'] === 0 || $r['attr16'] === ''){
echo '';
}else{
echo '"16"' . ' "' . $r['attr16'] . ' ; ' . $r['val16'] . '"' . "<br />";
}
echo "}";
echo "<br>";
}
}
echo "}";
echo "<br>";
}
echo "<p>" . '}' . "</p>";
What i'm trying to display the text as
custom_weapons_v3
{
STEAM_0:0:1621342
{
"0" //Bat
{
"level" "0"
"quality" "0"
"1" "23 ; 21"
"2" "231 ; 231"
"3" "231 ; 0"
}
"159" //Dalokohs Bar
{
"level" "4"
"quality" "2"
"1" "22 ; 32"
"2" "12 ; 42"
}
}
}
You can use file_put_contents(), but use PHP_EOL instead of <br />.
<?php
file_put_contents('test.html', "<html>". PHP_EOL ."<head><title>Test</title></head>". PHP_EOL ."<body>". PHP_EOL ."<h1>Hello World!</h1>". PHP_EOL ."</body>". PHP_EOL ."</html>");
?>
test.html will be,
<html>
<head><title>Test</title></head>
<body>
<h1>Hello World!</h1>
</body>
</html>
You can use the following code,
<?php
$data = "";
$data .= "custom_weapons_v3". PHP_EOL;
$data .= "{". PHP_EOL;
foreach($arr as $key => $a){
$data .= PHP_EOL
$check = $a['steamid'];
$data .= $check;
$data .= PHP_EOL . "{". PHP_EOL;
foreach($data as $key => $r){
-------------------
-------------------
}
}
$data .= "}" . PHP_EOL . "}";
file_put_contents('output.txt', $data);
?>
Related
Hello im having some problems with my forms. It says "Saw a form start tag, but there was already an active form element. Nested forms are not allowed." but when im looking in my code the forms are not even nested in my code can i have some help.
$sql8 = 'SELECT läggtill.serier, läggtill.id, läggtill.id2, läggtill.säsonger, läggtill.betyg, kategorier.kategori from läggtill inner join'
. ' kategorier on läggtill.kategorier=kategorier.id order by läggtill.serier';
$resultat8 = $mysqli->query($sql8);
$antal8 = $resultat8->num_rows;
while ($rad8 = $resultat8->fetch_assoc()) {
echo "<li>" .$rad8['serier'] .', ' .$rad8['säsonger'] .' Säsonger, ' .$rad8['kategori'] .'<br>'.
'Betyg: ' .$rad8['betyg'] .'/10 </li>' .'<br>';
$id = $rad8['id'];
?>
<?php
echo '<form action="alla.php" method="POST">
<button name=' .$id .'>Ta bort</button>
</form>'
?>
<?php
if (isset($_POST[$id])) {
$sql9 = "Delete from läggtill where id=" . $id . " LIMIT 1";
if ($resultat9 === $mysqli->query($sql9)) {
header('location: alla.php');
} else {
echo "det misslyckades.";
}
echo "asdasd";
}
$id2 = $rad8['id2'];
echo "<div class=" . 'Update' . '>'
?>
<?php
echo "<form action='alla.php' method='POST'>
<label for='serier'>Serie: </label>
<input type='text' name='serier' id='serier'><br>";
$sql14 = 'SELECT * FROM kategorier';
$resultat14 = $mysqli->query($sql14);
$antal14 = $resultat14->num_rows;
if ($antal14 == 0) {
echo 'Inget funnet';
} else {
$sql15 = 'SELECT kategori, id FROM kategorier';
$resultat15 = $mysqli->query($sql15);
$antal15 = $resultat15->num_rows;
if ($antal15 == 0) {
echo 'Inget kategori funnen';
} else {
echo 'Kategori: <select name =kategorier>';
while ($rad15 = $resultat15->fetch_assoc()) {
echo '<option value='. $rad15['id'] . '>' . $rad15['kategori'] . '</option><br>';
}
echo '</select>';
}
}
echo '<br><button name= . $id2' . '>Uppdatera</button>';
if (isset($_POST[$id2])) {
$kategorier = $_POST['kategorier'];
$betyg = $_POST['betyg'];
$serier = $_POST['serier'];
$säsonger = $_POST['säsonger'];
if(strlen($kategorier) && strlen($säsonger) && strlen($serier) && strlen($betyg)) {
$sql12 = '"UPDATE läggtill SET kategorier="" . $kategorier . "", säsonger="" . $säsonger . "", serier="" . $serier . "","
. " betyg="" . $betyg . "" WHERE id2="" . $id2 . "";';
if($resultat9 == $mysqli->query($sql12)) {
header('location: alla.php');
} else {
echo 'Det misslyckades';
}
}
}
}
echo "</form>";
echo "</div>";`**enter code here**`
check have updated few things, hope it will work out
<?php
$sql8 = 'SELECT läggtill.serier, läggtill.id, läggtill.id2, läggtill.säsonger, läggtill.betyg, kategorier.kategori from läggtill inner join'
. ' kategorier on läggtill.kategorier=kategorier.id order by läggtill.serier';
$resultat8 = $mysqli->query($sql8);
$antal8 = $resultat8->num_rows;
while ($rad8 = $resultat8->fetch_assoc()) {
echo "<li>" . $rad8['serier'] . ', ' . $rad8['säsonger'] . ' Säsonger, ' . $rad8['kategori'] . '<br>' .
'Betyg: ' . $rad8['betyg'] . '/10 </li>' . '<br>';
$id = $rad8['id'];
?>
<?php
echo '<form action="alla.php" method="POST">
<button name=' . $id . '>Ta bort</button>
</form>'
?>
<?php
if (isset($_POST[$id])) {
$sql9 = "Delete from läggtill where id=" . $id . " LIMIT 1";
if ($resultat9 === $mysqli->query($sql9)) {
header('location: alla.php');
} else {
echo "det misslyckades.";
}
echo "asdasd";
}
$id2 = $rad8['id2'];
echo "<div class=" . 'Update' . '>'
?>
<?php
echo "<form action='alla.php' method='POST'>
<label for='serier'>Serie: </label>
<input type='text' name='serier' id='serier'><br>";
$sql14 = 'SELECT * FROM kategorier';
$resultat14 = $mysqli->query($sql14);
$antal14 = $resultat14->num_rows;
if ($antal14 == 0) {
echo 'Inget funnet';
} else {
$sql15 = 'SELECT kategori, id FROM kategorier';
$resultat15 = $mysqli->query($sql15);
$antal15 = $resultat15->num_rows;
if ($antal15 == 0) {
echo 'Inget kategori funnen';
} else {
echo 'Kategori: <select name =kategorier>';
while ($rad15 = $resultat15->fetch_assoc()) {
echo '<option value=' . $rad15['id'] . '>' . $rad15['kategori'] . '</option><br>';
}
echo '</select>';
}
}
echo '<br><button name= . $id2' . '>Uppdatera</button></form>';
if (isset($_POST[$id2])) {
$kategorier = $_POST['kategorier'];
$betyg = $_POST['betyg'];
$serier = $_POST['serier'];
$säsonger = $_POST['säsonger'];
if (strlen($kategorier) && strlen($säsonger) && strlen($serier) && strlen($betyg)) {
$sql12 = '"UPDATE läggtill SET kategorier="" . $kategorier . "", säsonger="" . $säsonger . "", serier="" . $serier . "","
. " betyg="" . $betyg . "" WHERE id2="" . $id2 . "";';
if ($resultat9 == $mysqli->query($sql12)) {
header('location: alla.php');
} else {
echo 'Det misslyckades';
}
}
}
}
echo "</div>";
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;
}
I am trying to write data into a txt file, but It won't work. there is no error. it just won't work.
here is the code
if (isset($_REQUEST['submit']))
{
$hour = $_REQUEST['hour'];
$family = $_REQUEST['family'];
$how = $_REQUEST['how'];
$will = $_REQUEST['will'];
$fun = $_REQUEST['fun'];
$kind = $_REQUEST['kind'];
$device = $_REQUEST['device'];
$study = $_REQUEST['study'];
$agree = $_REQUEST['agree'];
$text = "hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind : " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />" ;
fopen('/1.txt', "r");
fwrite('/1.txt',"hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind : " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />");
fclose('/1.txt');
}
do note that there is an "else" that includes all the aforementioned input fields that I used in the code , and that I am executing on localhost.
thanks
You cannot write to it since you are opening it as read-only:
$handle = fopen('/1.txt', "r");
Instead:
$handle = fopen('/1.txt', "w"); // to write only, if you need to read and write use 'w+'
You also need to store fopen in a $handle so you can write to it later.
Documentation: http://www.php.net/manual/en/function.fopen.php
Now the first parameter for fwrite should be $handle:
fwrite($handle, "hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind : " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />");
Documentation: http://www.php.net/manual/en/function.fwrite.php
At the end you should also close with $handle:
fclose($handle);
Documentation: http://www.php.net/manual/en/function.fclose.php
You are opening the file as read-only, with the ruse w instead. And you need to pass fwrite the return of fopen as first argument, not the filename:
$f = fopen('/1.txt', "w");
fwrite($f,"hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind : " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />");
fclose($f);
You have to pass in a 'w' to write to a file.
$handle = fopen('/1.txt', "w");
Passing in 'r' makes it so you are opening a 'read-only' file.
Much simpler:
file_put_contents('/1.txt', $text);
hi i cant seem to get this to work and i keep getting undefined error in line 8 and 11. Here is my code
<?php
$count = 0;
$temp = 0;
$name = array("Suzuki", "Holden", "Jaguar", "Toyota", "Hyundai", "Ford", "Honda", "Mazda");
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . "<p>";
for ($incount = 0; $incount <= 7; $incount++) {
if ($name[$incount] > $name[$incount + 1]) {
$temp = $name[$incount];
$name[$incount] = $name[$incount + 1];
$name[$incount + 1] = $temp;
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . $count++ . "<p>";
}
}
?>
here is mistake:
$name[$incount] > $name[$incount + 1]
when $incount = 7 then $incount +1 = 8 what is undefined for you... because you defined just 7 elements...
solution can be just $incount < 7
You are not using the correct algorithm for bubble sort. It needs two loops.
<?php
$count = 0;
$temp = 0;
$name = array("Suzuki", "Holden", "Jaguar", "Toyota", "Hyundai", "Ford", "Honda", "Mazda");
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . "<p>";
for ($incount = 0; $incount <= 7; $incount++) {
for ($innercount = $incount+1; $innercount <= 7; $innercount++) {
if ($name[$incount] > $name[$innercount]) {
$temp = $name[$incount];
$name[$incount] = $name[$innercount];
$name[$innercount] = $temp;
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . $count++ . "<p>";
}
}
}
?>
<?php
$count = 0;
$temp = 0;
$name = array("Suzuki", "Holden", "Jaguar", "Toyota", "Hyundai", "Ford", "Honda", "Mazda");
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . "<p>";
for ($incount = 0; $incount < 8; $incount++) {
if(isset($name[$incount + 1]))
if ($name[$incount] > $name[$incount + 1]) {
$temp = $name[$incount];
$name[$incount] = $name[$incount + 1];
$name[$incount + 1] = $temp;}
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . $count++ . "<p>";
}
?>
But
Better Use
<?php
$count = 0;
$temp = 0;
$name = array("Suzuki", "Holden", "Jaguar", "Toyota", "Hyundai", "Ford", "Honda", "Mazda");
foreach($name as $f) echo $f." ";
echo "<p>";
for ($incount = 0; $incount < 8; $incount++) {
if(isset($name[$incount + 1]))
if ($name[$incount] > $name[$incount + 1]) {
$temp = $name[$incount];
$name[$incount] = $name[$incount + 1];
$name[$incount + 1] = $temp;}
foreach($name as $f) echo $f." ";
echo $count++ . "<p>";
}
?>
i have the following code in PHP
$link ="http://ws.audioscrobbler.com/2.0/?method=&user=xgayax" .
"&api_key=b25b959554ed76058ac220b7b2e0a026";
$xml = #simplexml_load_file($link);
$tracks = $xml->recenttracks->track;
for ($i = 0; $i < 3; $i++) {
$playingnow = $tracks[$i]->attributes()->nowplaying;
$name = $tracks[$i]->name;
$artist = $tracks[$i]->artist;
$url = $tracks[$i]->url;
$date = $tracks[$i]->date;
$img = $tracks[$i]->children();
$img = $img->image[0];
echo "<a href='" . $url . "' target='TOP'>";
if ($nowplaying == "true") {
echo "Now playing: ";
}
echo "<img src='" . $img . "' alt='album' />
$artist . " - " . $trackname . " # " . $date . "
</a>
";
}
and i got the following error
Parse error: syntax error, unexpected '#', expecting ',' or ';' on line 31
any solution for this problem???
Your last lines have a error, the correct code is:
echo "<img src='" . $img . "' alt='album' />" .
// ^^^ missing
$artist . " - " . $trackname . " # " . $date . "
</a>
";