Jump to content

[SOLVED] IS there away?


Cragsterboy

Recommended Posts

Hey,

 

Im working on my first php script from scratch and enjoying it :D 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  :P So yeh is there a way to do this sort of thing?

 

Thanks,

 

~Crag

Link to comment
https://forums.phpfreaks.com/topic/128096-solved-is-there-away/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/128096-solved-is-there-away/#findComment-663364
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/128096-solved-is-there-away/#findComment-663386
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/128096-solved-is-there-away/#findComment-663390
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/128096-solved-is-there-away/#findComment-663391
Share on other sites

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 :D

 

Thanks so much

 

~Crag

Link to comment
https://forums.phpfreaks.com/topic/128096-solved-is-there-away/#findComment-663410
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/128096-solved-is-there-away/#findComment-663519
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.