Cragsterboy Posted October 12, 2008 Share Posted October 12, 2008 Hey, Im working on my first php script from scratch and enjoying it lol But i was wondering if there was away to output information into a table, but the problem is the information i want to out put is a copy and paste from a game report so its like this: No hit Strike: Left shoulder - 12 HP No hit No hit No hit Strike: Head - 9 HP No hit So when it out puts i would like it to be in two different colums, so like it would be: No hit | Strike: Left shoulder - 12hp No hit | No Hit No Hit | Strike: Head - 9hp and so on. SO realy it should be every two lines of the inputted text is a row unless it starts with a - then its on the same line as before. I hope i havent confused you, i probs have acutaly So yeh is there a way to do this sort of thing? Thanks, ~Crag Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 Is this what you're looking for? http://www.phpfreaks.com/forums/index.php/topic,95426.0.html It's more detailed than I would go into Quote Link to comment Share on other sites More sharing options...
Cragsterboy Posted October 12, 2008 Author Share Posted October 12, 2008 its got the table element but it still doesnt answer my question I just need a code to turn every two lines inputted in to be a row unless the line starts with '-' then it would stay on the previous row of the table. I hope im making sense lol ~Crag Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 Ah, sorry, let me make an example of that. Is the info from a database, array, or what? Quote Link to comment Share on other sites More sharing options...
Cragsterboy Posted October 12, 2008 Author Share Posted October 12, 2008 heres the script so far: http://cakeisalie.info/westconverter/index.php people copy and paste their game reports into the text box then click 'submit' which changes the report into BBcode, i have done all that part fine. But i just need it to output into a table so it looks better when posted into a forum if you get me lol Thanks, ~Crag Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 Well this should work if the values are in an array... added comments so you can (hopefully) tell what's going on. $values = array( 'No hit', 'Strike: Left shoulder', '- 12 HP', 'No hit', 'No hit', 'Strike: Head', '- 9 HP', 'No hit', 'No hit', 'No hit' ); echo '<table><td>'; // Start a table $size = count($values); $valBreak = '</td><td>'; // Set the value that will be put between the values for($i = 0; $i < $size; ++$i) { echo $values[$i]; // See if the next value starts with '-' and output it if it is if(isset($values[$i+1]) && $values[$i+1][0] == '-') { echo ' ' . $values[$i+1]; ++$i; // Move the pointer up one since we've outputted 2 values this loop } // Output the between-item value if it isn't the last loop of the array if(isset($values[$i+1])) { echo $valBreak; } // Swap the between-item value $valBreak = ($valBreak == '</td><td>') ? '</td></tr><tr><td>' : '</td><td>'; } echo '</td></tr></table>'; // End the table Quote Link to comment Share on other sites More sharing options...
Cragsterboy Posted October 12, 2008 Author Share Posted October 12, 2008 ok ive added your code, but im getting this error: atal error: Cannot use string offset as an array in /public_html/westconverter/index.php on line 62 heres my script code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <?php $Player1 = $_POST["Player1"]; $Player2 = $_POST["Player2"]; $results = $_POST['results']; $test = preg_replace('/\d{1,3} HP/', '[size=4][color=Red][b]$0[/b][/color][/size]', $results); $words = array('Strike: Left arm', 'Strike: Right arm', 'Strike: Head', 'Strike: Left shoulder', 'Strike: Right shoulder', 'No hit'); $bbcode = array('[b][color=Red]Strike: Left arm[/color][/b]', '[b][color=Red]Strike: Right Arm[/color][/b]', '[b][color=Red]Strike: Head[/color][/b]', '[b][color=Red]Strike: Left Shoulder[/color][/b]', '[b][color=Red]Strike: Right Shoulder[/color][/b]', '[color=Green][b]No Hit[/b][/color]'); $bbword = str_replace($words, $bbcode, $test); if (!isset($_POST['Submit'])) { ?> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Duel Report Converter</title> </head> <body> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label>Duel: <input type="text" name="Player1" /> </label> <label>vs <input type="text" name="Player2" /> </label> <p>Results:</p> <p> <label> <textarea name="results" cols="100" rows="10"></textarea> </label> </p> <p> <label> <input type="Submit" name="Submit" value="Submit" /> </label> </p> <p> </p> <p>BBCode:</p> <p> <? } else { echo "[b]Duel:[/b]<br /><br /> ".$Player1."[b] vs [/b]".$Player2."<br /><br />"; echo "[b]Results:[/b]<br /><br />"; echo '<table><td>'; // Start a table $size = count($bbword); $valBreak = '</td><td>'; // Set the value that will be put between the values for($i = 0; $i < $size; ++$i) { echo $bbword[$i]; // See if the next value starts with '-' and output it if it is if(isset($bbword[$i+1]) && $bbword[$i+1][0] == '-') { echo ' ' . $bbword[$i+1]; ++$i; // Move the pointer up one since we've outputted 2 values this loop } // Output the between-item value if it isn't the last loop of the array if(isset($bbword[$i+1])) { echo $valBreak; } // Swap the between-item value every other loop $valBreak = ($valBreak == '</td><td>') ? '</td></tr><tr><td>' : '</td><td>'; } echo '</td></tr></table>'; // End the table } ?> </form> </p> <p> </p> </body> </html> See if you can make sense of it for me lol Thanks so much, ~Crag Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 try adding the line $bbcode = explode("\n", trim($bbcode)); to make it an array before using the code I gave you... This is assuming of course that the reports are as you've posted in the quote in your first post. Try to figure out how the code I gave you works too Quote Link to comment Share on other sites More sharing options...
Cragsterboy Posted October 12, 2008 Author Share Posted October 12, 2008 ok ive tried putting your code in a number of places, but still get the same output error. Like i said in my first post this is my first time of making php from scratch and so if you could give me more guidance that would be brilliant Thanks so much ~Crag Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 Oh, I'm sorry, I gave you the wrong thing it should be $bbword = explode("\n", trim($bbword)); I promise I went to get some caffeine, so hopefully I'll stop making little mistakes. Quote Link to comment Share on other sites More sharing options...
Cragsterboy Posted October 12, 2008 Author Share Posted October 12, 2008 ahh brilliant its worked now Ok sorry but one last thing, ive added a text box to the page and i would like it to output into the text box instead of creating a new page, is there away to do this? Thanks, ~Crag Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 Well yes, you could put the form handling and the input text box on the same page (if they aren't already) and echo the output between the <textbox> and </textbox> tags of the destination textbox. Quote Link to comment Share on other sites More sharing options...
Cragsterboy Posted October 12, 2008 Author Share Posted October 12, 2008 i have surround the output with textarea tags but it still outputs onto a different page, very annoying. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 replace <textarea name="results" cols="100" rows="10"></textarea> with <textarea name="results" cols="100" rows="10"><?php echo (isset($_POST['results'])) ? $_POST['results'] : '' ?></textarea> Quote Link to comment Share on other sites More sharing options...
Cragsterboy Posted October 12, 2008 Author Share Posted October 12, 2008 ok tried what you said didnt work but did it another way to output into a text box but not on the same page, but the problem was it came up with all the coding for the table as well. So is there anyway to make it output the code onto the same page? Thanks, ~Crag Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted October 12, 2008 Share Posted October 12, 2008 If you mean "can you do it without the page appearing to refresh", you could do it with ajax. Quote Link to comment Share on other sites More sharing options...
Cragsterboy Posted October 12, 2008 Author Share Posted October 12, 2008 dont worry ive done it now i removed some codes and it does it woop! Thanks so much for your help! ~Crag Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.