7 Replies - 661 Views - Last Post: 12 June 2009 - 03:52 AM Rate Topic: -----

#1 KICKIE  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-May 09

How to do the division within a string?

Posted 12 June 2009 - 12:28 AM

How could I make a division with this string....

('99999','"," 28SB7"," 2009 06 03 12 43","10","23","xxx')

marks = 10 / 23
Is This A Good Question/Topic? 0
  • +

Replies To: How to do the division within a string?

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: How to do the division within a string?

Posted 12 June 2009 - 01:42 AM

View PostKICKIE, on 11 Jun, 2009 - 11:28 PM, said:

How could I make a division with this string....

('99999','"," 28SB7"," 2009 06 03 12 43","10","23","xxx')

marks = 10 / 23

Didn't that string come from an array?

If so then..
$marks = (int)$array[4] / (int)$array[5];


If not then you'll have to RESPLIT it somehow to get the values....... Probably you'd use explode() but those quotes are somewhat unmatched ("xxx'?).
Was This Post Helpful? 0
  • +
  • -

#3 KICKIE  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-May 09

Re: How to do the division within a string?

Posted 12 June 2009 - 02:40 AM

Now I'm trying to do the division between the last two set of numbers to get the percentage of note, and I've cut the ending 'xxx' by the following code and I've got the result like this.....
10/23
10
23
0.43

51.33/118
10
23
0.43

10/24
10
23
0.43

' 99999 ',' 28SB7',' 2009 06 03 12 43','10','23'
' 99999 ',' Certification 1 : niveau assistant toiletteur-zoocosm�tologue',' 2009 06 04 9 40','51.33','118'
' 99999 ',' 29C5',' 2009 06 04 12 40','10','24'

[code]
<?php
// Switch between HTML output and normal
$HTML = true;

// Read the file..
$Contents = file_get_contents("importfile.txt");

// Split the file into an array based on a single line
$ContentArray = explode("\r\n", $Contents);
$ContentArrayFixed = array();

function division($mark)
{
$mark = $ContentArrayFixed[3] / $ContentArrayFixed[4];
echo $mark;
}

// Go through the array and seperate it based on commas in the file
foreach($ContentArray as $ContentElement)
{
$Items = explode(",", $ContentElement);
foreach($Items as $Item)
{
if($Item != "") // If it's not blank...
{
if(strpos($Item, "/") === false) // Find position of first occurrence of a string
$ContentArrayFixed[] = $Item; // breakup the arrays

else
{
$Arr = explode("/", $Item); // Explode the array with a / in it <notes>
echo $Item . "<br/>";
foreach($Arr as $Ele)
$ContentArrayFixed[] = $Ele; // $Ele equals to the maxNotes field
$Mark = $ContentArrayFixed[3] / $ContentArrayFixed[4] ;
echo $ContentArrayFixed[3] . "<br/>";
echo $ContentArrayFixed[4] . "<br/>";
//$Mark = $Ele;
echo number_format($Mark,2) . "<br/>" . "<br/>";

}
}
}
}

$FixedLines = array();
$CurrentLine = array();

foreach($ContentArrayFixed as $FixedItem)
{
$CurrentLine[] = $FixedItem;

if($FixedItem == "xxx") // End of line
{
$FixedLines[] = $CurrentLine;
$CurrentLine = array();
}
}

// Now output the CSV
foreach($FixedLines as $FixedLine)
{
$Line = implode("','", $FixedLine);
$Line = substr($Line, 0, -6);
echo "'" . $Line . "'" . ($HTML?"<br />":"\r\n");
file_put_contents("output.csv", "'" . $Line . "'" . "\t\n", FILE_APPEND);
}
?>
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: How to do the division within a string?

Posted 12 June 2009 - 03:31 AM

So you have got it working now?
Was This Post Helpful? 0
  • +
  • -

#5 KICKIE  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-May 09

Re: How to do the division within a string?

Posted 12 June 2009 - 03:35 AM

No the result is only the first array in return....
Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: How to do the division within a string?

Posted 12 June 2009 - 03:40 AM

Well that code's slightly confusing (I know I wrote it but stuffs changed)...

There should only be two elements in $Arr, since the string will just be "10/23".

Try this:
$Arr = explode("/", $Item); // Explode the array with a / in it <notes>
echo $Item . "<br/>"; 
foreach($Arr as $Ele) 
$ContentArrayFixed[] = $Ele; // $Ele equals to the maxNotes field
$Mark = $Arr[0] / $Arr[1];
echo $Arr[0] . "<br/>"; 
echo $Arr[1] . "<br/>";
//$Mark = $Ele;
echo number_format($Mark,2) . "<br/>" . "<br/>";


Aside from that I don't see why it wouldn't work.... Perhaps I need another coffee.

This post has been edited by MageUK: 12 June 2009 - 03:40 AM

Was This Post Helpful? 0
  • +
  • -

#7 KICKIE  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-May 09

Re: How to do the division within a string?

Posted 12 June 2009 - 03:49 AM

It works fine with your code, it 's me who need another big cup of coffee.
Now I try to put the mark back to the end of the string.
Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#8 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: How to do the division within a string?

Posted 12 June 2009 - 03:52 AM

Hehe :)

Well I'm glad it's sorted.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1