• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Translate .asp routine into .php (partially done)

I am translating this simple .ASP routine to .php:
.ASP Version
<%
mend=".asp"
topkeywords="Phrase 1, Phrase 2, Phrase 3, Phrase 4, Phrase 5, Phrase 6, Phrase 7, Phrase 8"
topkwarray=split(topkeywords,", ")
kwcnt=ubound(topkwarray)
mtopics=rankeylinks(8,", ")

Response.write mtopics

public function rankeylinks(howmany,delimiter)
  done=0
  rankeylinks=""
  while done < howmany
    mcurrent=""
    ncurrent=""
    Randomize
    mcurrent=topkwarray(Int((kwcnt)*Rnd)+0)
    ncurrent=replace(mcurrent," ","-",1,-1,1)
    zlink="<a href='" + ncurrent + mend + "'>" + mcurrent + "</a>"
    rankeylinks=rankeylinks & zlink
    done = done + 1
    if done < howmany then
      rankeylinks=rankeylinks & delimiter
    End if
  Wend
End Function
%>

The workings of this can be seen here:
http://www.4gs.org/asp/test.asp
It produces a random set of links.

Now, I've started on the translation to .php below, but it errors on line 12:
<?php

$mend=".php";
$topkeywords="Phrase 1, Phrase 2, Phrase 3, Phrase 4, Phrase 5, Phrase 6, Phrase 7, Phrase 8";
$topkwarray=explode(", ",$topkeywords);
$kwcnt = count($topkwarray);
$mtopics=rankeylinks(6,", ");

echo $mtopics;

function rankeylinks($howmany,$delimiter) {
  $done=0;
  while $done < $howmany;
    $mcurrent="";
    $ncurrent="";
    $mcurrent=$topkwarray[floor(($kwcnt)*Rnd)+0];
    $ncurrent=str_replace(" ","-",1,-1,1,$mcurrent);
    $zlink="<a href='" + $ncurrent + $mend + "'>" + $mcurrent + "</a>";
    rankeylinks=rankeylinks.$zlink;
    $done = $done + 1;
    if ($done < $howmany) {
      rankeylinks=rankeylinks.$delimiter;
    }
  }
}
?>

Your help will be greatly appreciated.
Thanks, Greg Schipper www.AdventDigital.net Phone: 210-493-6193
0
gregsschipper
Asked:
gregsschipper
  • 10
  • 5
1 Solution
 
Rok-KraljCommented:
<?php

$mend=".php";
$topkeywords="Phrase 1, Phrase 2, Phrase 3, Phrase 4, Phrase 5, Phrase 6, Phrase 7, Phrase 8";
$topkwarray=explode(", ",$topkeywords);
$kwcnt = count($topkwarray);
$mtopics=rankeylinks(6,", ");

echo $mtopics;

function rankeylinks($howmany,$delimiter) {
  $done=0;
  while ($done < $howmany) {
    $mcurrent="";
    $ncurrent="";
    $mcurrent=$topkwarray[floor(($kwcnt)*Rnd)+0];
    $ncurrent=str_replace(" ","-",1,-1,1,$mcurrent);
    $zlink="<a href='" + $ncurrent + $mend + "'>" + $mcurrent + "</a>";
    rankeylinks=rankeylinks.$zlink;
    $done = $done + 1;
    if ($done < $howmany) {
      rankeylinks=rankeylinks.$delimiter;
    }
  }
}
}
?>

  while $done < $howmany;

there is no semicolon
0
 
Rok-KraljCommented:
<?php

$mend=".php";
$topkeywords="Phrase 1, Phrase 2, Phrase 3, Phrase 4, Phrase 5, Phrase 6, Phrase 7, Phrase 8";
$topkwarray=explode(", ",$topkeywords);
$kwcnt = count($topkwarray);
$mtopics=rankeylinks(6,", ");

echo $mtopics;

function rankeylinks($howmany,$delimiter) {
  $done=0;
  while ($done < $howmany) {
    $mcurrent="";
    $ncurrent="";
    $mcurrent=$topkwarray[floor(($kwcnt)*Rnd)+0];
    $ncurrent=str_replace(" ","-",1,-1,1,$mcurrent);
    $zlink="<a href='" + $ncurrent + $mend + "'>" + $mcurrent + "</a>";
    rankeylinks=rankeylinks.$zlink;
    $done = $done + 1;
    if ($done < $howmany) {
      rankeylinks=rankeylinks.$delimiter;
    }
  }
}
?>
0
 
gregsschipperAuthor Commented:
I tried both solutions, thank you.
However, I get a parse error on line 19
Look at the code running here http://www.4gs.org/asp/test.php 
Thanks, Greg Schipper www.AdventDigital.net Phone: 210-493-6193
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Rok-KraljCommented:
rankeylinks=rankeylinks.$zlink;

change

$rankeylinks=$rankeylinks.$zlink;
0
 
Rok-KraljCommented:
and a few more:

<?php

$mend=".php";
$topkeywords="Phrase 1, Phrase 2, Phrase 3, Phrase 4, Phrase 5, Phrase 6, Phrase 7, Phrase 8";
$topkwarray=explode(", ",$topkeywords);
$kwcnt = count($topkwarray);
$mtopics=rankeylinks(6,", ");

echo $mtopics;

function rankeylinks($howmany,$delimiter) {
  $done=0;
  while ($done < $howmany) {
    $mcurrent="";
    $ncurrent="";
    $mcurrent=$topkwarray[floor(($kwcnt)*Rnd)+0];
    $ncurrent=str_replace(" ","-",1,-1,1,$mcurrent);
    $zlink="<a href='" + $ncurrent + $mend + "'>" + $mcurrent + "</a>";
    $rankeylinks=$rankeylinks.$zlink;
    $done = $done + 1;
    if ($done < $howmany) {
      $rankeylinks=$rankeylinks.$delimiter;
    }
  }
}
?>
0
 
Rok-KraljCommented:
and you still have problems with line 17:

    $ncurrent=str_replace(" ","-",1,-1,1,$mcurrent);

http://si.php.net/str_replace

str replace accepts 3 parameters (+1 optional), you gave 6.
0
 
gregsschipperAuthor Commented:
Thank you.
Current Version is, no output:

<?php
$mend=".php";
$topkeywords="Phrase 1, Phrase 2, Phrase 3, Phrase 4, Phrase 5, Phrase 6, Phrase 7, Phrase 8";
$topkwarray=explode(", ",$topkeywords);
$kwcnt = count($topkwarray);
$mtopics=rankeylinks(6,", ");
echo $mtopics;
function rankeylinks($howmany,$delimiter) {
  $done=0;
  while ($done < $howmany) {
    $mcurrent="";
    $ncurrent="";
    $mcurrent=$topkwarray[floor(($kwcnt)*Rnd)+0];
    $ncurrent=str_replace(" ","-",$mcurrent);
    $zlink="<a href='" + $ncurrent + $mend + "'>" + $mcurrent + "</a>";
    $rankeylinks=$rankeylinks.$zlink;
    $done = $done + 1;
    if ($done < $howmany) {
      $rankeylinks=$rankeylinks.$delimiter;
    }
  }
}
?>

Thanks, Greg Schipper www.AdventDigital.net Phone: 210-493-6193
0
 
Rok-KraljCommented:
Works it ok?
0
 
gregsschipperAuthor Commented:
No, it has no output.
When we have it right, both will have similar output.
.asp version: http://www.4gs.org/asp/test.asp
.php version: http://www.4gs.org/asp/test.php

Thanks, Greg Schipper www.AdventDigital.net Phone: 210-493-6193
0
 
Rok-KraljCommented:
What is your function rankeylinks all about?
0
 
gregsschipperAuthor Commented:
It sends back random links.
Thanks, Greg Schipper www.AdventDigital.net Phone: 210-493-6193
0
 
gregsschipperAuthor Commented:
Hello Rak-Kralj:
Pse don't give up. It is almost done... Thanks, Greg
0
 
Rok-KraljCommented:
Sorry, even experts need to sleep.

Can you please post your function in asp and php....

So looks like you have function rankeylinks and variable rankeylinks.

      rankeylinks=rankeylinks & delimiter
0
 
Rok-KraljCommented:
You had to wait so long, because I wrote you a totally new function, highly optimized and IT WORKS!

<?php
$end=".php";
$keywords="Phrase 1, Phrase 2, Phrase 3, Phrase 4, Phrase 5, Phrase 6, Phrase 7, Phrase 8";
$array=explode(", ",$keywords);
$count=count($array);

function rankeylinks($howmany,$delimiter) {
global $array, $count, $end;

for ($i=0;$i<$howmany;++$i) {

    $current=$array[mt_rand(0,$count-1)];

$return.="<a href='".str_replace(" ","-",$current).$end."'>".$current."</a>".$delimiter;
}

return(substr($return,0,strlen($delimiter)*(-1)));

}

echo(rankeylinks(8,", "));
?>

Your php code has a lot of mistakes:
- you must use . (dot) for merging strings not + (plus)
- you must globalize variables out of function (global $array, $count, $end;)
....
0
 
Rok-KraljCommented:
Script uploaded on my server: http://www.midnightwars.com/hp.php
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now