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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.