kiwis-are-cool Posted August 14, 2013 Share Posted August 14, 2013 I'm trying 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" } } } Link to comment https://forums.phpfreaks.com/topic/281147-write-a-large-amount-of-data-to-a-text-file/ Share on other sites More sharing options...
PravinS Posted August 14, 2013 Share Posted August 14, 2013 if your content is big then open your file in append mode and write each line by line in file as you are creating text file the HTML tags will be written in file as it is, so instead of <br> tags use \n for new line Link to comment https://forums.phpfreaks.com/topic/281147-write-a-large-amount-of-data-to-a-text-file/#findComment-1444926 Share on other sites More sharing options...
kiwis-are-cool Posted August 14, 2013 Author Share Posted August 14, 2013 How do you input php? Would it just be <?php echo "<p>" . 'custom_weapons_v3' . "</p>"; ?> Link to comment https://forums.phpfreaks.com/topic/281147-write-a-large-amount-of-data-to-a-text-file/#findComment-1444930 Share on other sites More sharing options...
PravinS Posted August 14, 2013 Share Posted August 14, 2013 How do you input php? Would it just be <?php echo "<p>" . 'custom_weapons_v3' . "</p>"; ?> i didn't get your query Link to comment https://forums.phpfreaks.com/topic/281147-write-a-large-amount-of-data-to-a-text-file/#findComment-1444933 Share on other sites More sharing options...
kiwis-are-cool Posted August 14, 2013 Author Share Posted August 14, 2013 query = "SELECT * FROM weapons ORDER BY steamid"; if ($stmt = $mysqli->prepare($query)) { $stmt->execute(); $stmt->bind_result($id, $steamid, $wepid, $weplvl, $weprare, $attr1, $attr2, $attr3, $attr4, $attr5, $attr6, $attr7, $attr8, $attr9, $attr10, $attr11, $attr12, $attr13, $attr14, $attr15, $attr16, $val1, $val2, $val3, $val4, $val5, $val6, $val7, $val8, $val9, $val10, $val11, $val12, $val13, $val14, $val15, $val16); while ($row = $stmt->fetch()) { $wep = $wepid; $wep = wepname($wep); $steamids[] = array( 'steamid' => $steamid ); $data[] = array( 'steam' => $steamid, 'wepid' => $wepid, 'weplvl' => $weplvl, 'wepname' => $wep, 'weprare' => $weprare, 'attr1' => $attr1, 'attr2' => $attr2, 'attr3' => $attr3, 'attr4' => $attr4, 'attr5' => $attr5, 'attr6' => $attr6, 'attr7' => $attr7, 'attr8' => $attr8, 'attr9' => $attr9, 'attr10' => $attr10, 'attr11' => $attr11, 'attr12' => $attr12, 'attr13' => $attr13, 'attr14' => $attr14, 'attr15' => $attr15, 'attr16' => $attr16, 'val1' => $val1, 'val2' => $val2, 'val3' => $val3, 'val4' => $val4, 'val5' => $val5, 'val6' => $val6, 'val7' => $val7, 'val8' => $val8, 'val9' => $val9, 'val10' => $val10, 'val11' => $val11, 'val12' => $val12, 'val13' => $val13, 'val14' => $val14, 'val15' => $val15, 'val16' => $val16, ); } $stmt->close(); } Link to comment https://forums.phpfreaks.com/topic/281147-write-a-large-amount-of-data-to-a-text-file/#findComment-1444935 Share on other sites More sharing options...
PravinS Posted August 14, 2013 Share Posted August 14, 2013 How do you input php? Would it just be <?php echo "<p>" . 'custom_weapons_v3' . "</p>"; ?> sorry not the SQL query, but what you try to say also <p> tag will also not work in text file and what is "custom_weapons_v3", if it is a PHP variable then use $custom_weapons_v3 Link to comment https://forums.phpfreaks.com/topic/281147-write-a-large-amount-of-data-to-a-text-file/#findComment-1444939 Share on other sites More sharing options...
kiwis-are-cool Posted August 14, 2013 Author Share Posted August 14, 2013 I chose the wrong example. This would be what I need to post, the full foreach loop is in my first post. 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 />"; } Link to comment https://forums.phpfreaks.com/topic/281147-write-a-large-amount-of-data-to-a-text-file/#findComment-1444942 Share on other sites More sharing options...
kiwis-are-cool Posted August 14, 2013 Author Share Posted August 14, 2013 Thanks for the advice Pravin, I figured it out! Link to comment https://forums.phpfreaks.com/topic/281147-write-a-large-amount-of-data-to-a-text-file/#findComment-1444945 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.