How To Parts Of Speech Only One Time In PHP - php

I want to show only one time the heading of parts of speeches for example when user enter the word "Spell" all the parts of speeches and its meaning come every time .
The headings are repeating multiple times . i want to print only one time
There should be a heading Of Noun,Verb,Adjective etc..
like this
Noun.
Verb.
adjective.
this is my code
while($row=mysql_fetch_array($sql)){
echo "<div id='wordid' style='display:none'>".$row["wordid"]."</div>";
echo $count++." : ".$row['definition']. " ";
if($row['pos']=="n"){
echo "(Noun) <br/>";
}
if($row['pos']=="s"){
echo "(Subject) <br/>";
}
if($row['pos']=="p"){
echo "(Proverb) <br/>";
}
if($row['pos']=="a"){
echo "(Adjective) <br/>";
}
if($row['pos']=="v"){
echo "(Verb) <br/>";
}
}

You can keep track of what the last wordid was and only echo the header if it changed.
// initialize to avoid notices
$lastWordId = '';
while($row=mysql_fetch_array($sql)){
if ($lastWordId != $row['wordid']) {
echo "<div id='wordid' style='display:none'>".
$row["wordid"].
"</div>";
$lastWordId = $row['wordid'];
}
echo $count++." : ".$row['definition']. " ";
if($row['pos']=="n"){
echo "(Noun) <br/>";
}
if($row['pos']=="s"){
echo "(Subject) <br/>";
}
if($row['pos']=="p"){
echo "(Proverb) <br/>";
}
if($row['pos']=="a"){
echo "(Adjective) <br/>";
}
if($row['pos']=="v"){
echo "(Verb) <br/>";
}
}

Related

removing multiple files from a web-directory using foreach() function

I want to delete multiple files from my directory, and for this I am using the following code
$x=array(".index.php",".code.html","about.txt");
foreach($x as $a)
unlink($a);
The wired thing with this code is that it sometime works and sometimes doesn't, and no errors.
Is there anything that I am missing?
Add some monitoring to your code, to see what happens:
foreach($x as $a) {
echo "File $a ";
if (file_exists($a)) {
if (is_file($a)) {
echo "is a regular file ";
} else {
echo "is not a regular file ";
}
if (is_link($a)) {
echo "is a symbolic link ";
}
if (is_readable($a)) {
echo " readable";
} else {
echo " NOT readable";
}
if (is_writeable($a)) {
echo " and writeable ";
} else {
echo " and NOT writeable ";
}
echo "owned by ";
echo posix_getpwuid(fileowner($a)) ['name'];
if (unlink($a)) {
echo "- was removed<br />\n";
} else {
echo "- was NOT removed<br />\n";
}
} else {
echo "doesn't exist<br />\n";
}
}
Also read this comment on the PHP manual page about unlinking files.
If you have to use a path for your file, convert it to a real path with the function realpath() - see https://php.net/manual/en/function.realpath.php

Adjusting last iteration in a foreach loop in PHP

I have a question and answer forum. A user may browse responses to questions and promote them if they wish. The code below outputs the different users that have promoted a given question - the users are in the array '$promoters' that is returned from the line $promoters = Promotion::find_all_promotions_for_response($response->response_id); . The foreach loop then goes through each and outputs them, styles by the css class "promoter list". Problem is this.....if I have one promoter I just want their name outputed (no problem with this). But if I have many I want to put a comma between each name and then none after the last name in the foreach loop. Straightforward problem but I failed to achieve this....I tried to add a count($promoters) line in an elseif condition so that if the array $promoters has more than one value, then it outputs the users fullname and then a comma, but of course the last name also has a comma after it which is wrong. How do you indentify the last iteration in a foreach loop and ask it to do something different with this.....
Many thanks guys...
<?php
$promoters = Promotion::find_all_promotions_for_response($response->response_id);
if(!empty($promoters)){
echo "<span class=\"promoted_by\">Promoted by </span>";
foreach($promoters as $promoter){
echo "<span class=\"promoter_list\">" . User::full_name($promoter->user_id) . ", </span>";
}
} else {
echo "";
};
?>
<?php
$promoters = Promotion::find_all_promotions_for_response($response->response_id);
if(!empty($promoters)){
echo "<span class=\"promoted_by\">Promoted by </span>";
foreach($promoters as $idx=>$promoter){
echo "<span class=\"promoter_list\">" . User::full_name($promoter->user_id);
if($idx < count($promoters) - 1) {
echo ", ";
}
echo "</span>";
}
} else {
echo "";
}
?>
UPDATE:
This is another way to do it using implode as suggested by #deceze:
<?php
$promoters = Promotion::find_all_promotions_for_response($response->response_id);
if(!empty($promoters)){
echo "<span class=\"promoted_by\">Promoted by </span>";
$htmlParts = array();
foreach($promoters as $idx=>$promoter){
$htmlParts[] = "<span class=\"promoter_list\">" . User::full_name($promoter->user_id);
}
echo implode(', </span>', $htmlParts) . '</span>';
} else {
echo "";
}
?>

Output xml result into table using PHP

I have successfully put together a PHP script to read content of an xml file and output the result to HTML page. The only bit I am struggling with is how to format the output into a table.
PHP Script:
<?php
// Loading the XML file
$xml = simplexml_load_file("ftpxml.xml");
echo "<h2>".$xml->getName()."</h2><br />";
foreach($xml->children() as $ftpxml)
{
echo "PID : ".$ftpxml->attributes()->pid."<br />";
echo "Account : ".$ftpxml->attributes()->account." <br />";
echo "Time : ".$ftpxml->attributes()->time." <br />";
echo "<hr/>";
}
?>
HTML Result:
PID : 279
Account : account001
Time : 137
----------------------------------------------------------------
PID : 268
Account : account002
Time : 301
----------------------------------------------------------------
PID : 251
Account : account003
Time : 5
----------------------------------------------------------------
I am lost as to how to display each table headings and the corresponding contents. I am new to PHP so please guide me or if already answered else where, please provide link so I can learn from it.
Thanks
<?php
// Loading the XML file
$xml = simplexml_load_file("ftpxml.xml");
echo "<h2>".$xml->getName()."</h2><br />";
echo "<table>";
foreach($xml->children() as $ftpxml)
{
echo "<tr><td>PID : ".$ftpxml->attributes()->pid."</td></tr>";
echo "<tr><td>Account : ".$ftpxml->attributes()->account." </td></tr>";
echo "<tr><td>Time : ".$ftpxml->attributes()->time." </td></tr>";
}
echo "</table>";
?>
echo '<table>';
echo '<thead><tr><th>PID</th><th>Account</th><th>Time</th></tr></thead><tbody>';
foreach($xml->children() as $ftpxml)
{
echo '<tr>';
echo "<td>PID : ".$ftpxml->attributes()->pid."</td>";
echo "<td>Account : ".$ftpxml->attributes()->account."</td>";
echo "<td>Time : ".$ftpxml->attributes()->time." </td>";
echo '</tr>';
}
echo '</tbody></table>';
I assume you want to make the different values different fields in the table:
// Loading the XML file
$xml = simplexml_load_file("ftpxml.xml");
echo "<h2>".$xml->getName()."</h2><br />";
echo "<table><thead><tr><th>PID</th><th>Account</th><th>Time</th></tr></thead><tbody>";
foreach($xml->children() as $ftpxml)
{
echo '<tr>';
echo '<td>' . $ftpxml->attributes()->pid . "</td>";
echo '<td>' . $ftpxml->attributes()->account . "</td>";
echo '<td>' . $ftpxml->attributes()->time . "</td>";
echo '</tr>';
}
echo '</tbody></table>';

How to speedup by code?

is there a way to speed up my code? It takes about 15 seconds to load ... I don't really see a way to reduce my code... I just thought about inserting the values into database, so the user does not have to load new info every time.. but the thing is that my cron only allows 1 load per hour ... by loading new info on every load it gives me fresh information..
$q1=mysql_query("SELECT * FROM isara");
while($r1=mysql_fetch_array($q1)){
$named=$r1['name'];
$idd=$r1['id'];
$descd=$r1['desc'];
$online=check_online($named);
$char = new Character($r1['name'],$r1['id'],$r1['desc']);
if($online == "online"){
$char->rank = $i++;
}
else{
$char->rank = 0;
}
$arr[] = $char;
}
?>
<br />
<h2 style="color:green">Online enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank>=1){
echo "<a style=\"color:green\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
<br />
<h2 style="color:red">Offline enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank==0){
echo "<a style=\"color:red\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
As I wrote in the comment, fetch the page once instead of once for every name in the database.
Pseudo code for my comment:
users = <get users from database>
webpage = <get webpage contents>
for (user in users)
<check if user exists in webpage>
As mentioned in the comments you're calling a webpage for each entry in your database, assuming that's about 2 seconds per call it's going to slow you down a lot.
Why don't you call the page once and pass the contents of it into the check_online() function as a parameter so your code would look something like this which will speed it up by quite a few magnitudes:
$content=file_get_contents("http://www.tibia.com/community/?subtopic=worlds&worl‌​d=Isara",0);
$q1=mysql_query("SELECT * FROM isara");
while($r1=mysql_fetch_array($q1)){
$named=$r1['name'];
$idd=$r1['id'];
$descd=$r1['desc'];
$online=check_online($named,$content);
$char = new Character($r1['name'],$r1['id'],$r1['desc']);
if($online == "online"){
$char->rank = $i++;
}
else{
$char->rank = 0;
}
$arr[] = $char;
}
?>
<br />
<h2 style="color:green">Online enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank>=1){
echo "<a style=\"color:green\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
<br />
<h2 style="color:red">Offline enemies</h2>
<?php
foreach ($arr as $char) {
if($char->rank==0){
echo "<a style=\"color:red\" href=\"http://www.tibia.com/community/?subtopic=characters&name=$char->name\">";
echo $char->name." ";
echo "</a>";
echo level($char->name)."<b> ";
echo vocation($char->name)."</b> (<i>";
echo $char->desc." </i>)<br />";
}
}
?>
and your check_online() function would look something like this:
function check_online($name,$content){
$count=substr_count($name, " ");
if($count > 0){ $ex=explode(" ",$name); $namez=$ex[1]; $nameused=$namez; }
else{ $nameused=$name; }
if(preg_match("/$nameused/",$content)){ $status="online"; }
else{ $status="offline"; }
return $status;
}
You can also do the following to make it faster
Stop using select * which is very bad on innodb
Put better indexes on your database to make the recordset return faster
Install PHP 5.4 as it's faster especially as you're creating a new object in each iteration
Use a byte code accelerator/cache such as xdebug
you should avoid using distinct (*) keyword in your SQL Query
for more information read this http://blog.sqlauthority.com/category/sql-coding-standards/page/2/

PHP/MySQL show last updated/most recent date

I have a mysql query that brings in a list of results, say 10, each with its own 'last edited' date, and was wondering if it was possible to display/echo/print ONLY the most recent of these dates using php?
Now I know I could do this with a mysql query, but I need to display all of the available results, but only 1 date, (the most recent).
Hope this makes sense. Any help greatly appreciated. S.
<?php
$availQuery=mysql_query("select * from isavailability");
$availResult=mysql_fetch_array($availQuery);
$date = $availResult['editDate'];
$newDate = date('D dS F Y',strtotime($date));
$time = date('G:i:s',strtotime($date));
echo '<p>This page was last updated on '.$newDate.' at '.$time.'</p>' . PHP_EOL;
while($availR=mysql_fetch_array($availQuery)) {
echo '<tr class="rows">'. PHP_EOL;
echo '<td><p>'.$availR['title'].'</p></td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==1) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==2) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==3) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '</tr>'. PHP_EOL;
}
?>
It can be done in a single query: SELECT isavailabilit.*, MAX(editDate) AS LastEditDate FROM isavailabilit;
Use it this way in the loop: $availR['LastEditDate']
If your query is ordered by your timestamp then you could just grab the first one - if I understand your question:
$myDate = $myData[0]['myDateColumn'];
foreach($myData as $row)
{
echo $myDate . ' ' . $row['someotherColumn'] . "\n";
}
or something similar? Presuming your results are in an array.

Categories