Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
please help me with code to check if numbers in a table is prime: this is my code below. I used the isPrime function but I get an error where by when I call the function in the table it returns the words "isPime"
<?php
$x="num1";
$y="num2";
if(isset($_POST['Submit'])) {
$start=$_POST['num1'];
$end=$_POST['num2'];
echo "<table width='250' cellpadding='2' cellspacing='4' border='1'>";
echo "<tr><td>Number</td><td>Odd or Even?</td><td>Prime?</td></tr>";
for ($start;$start<=$end;$start++) {
$answer=$start;
$number=$answer;
$check="text";
$num=$answer;
if($number%2==0) {
$check="Even";
} else {
$check="Odd";
}
{ }
echo "<tr><td>$answer</td><td>$check</td><td>
<?php ?>
</td></tr>";
} echo "</table>";
} ?>
You are missing the entire prime check and you aren't even printing anything in your third column.
Also someone seems to have deleted your code in the first EDIT and changed it in the second. I somehow had a copy of the first edit and could make an example.
This is a working example within your code:
<?php
$x="num1"; $y="num2";
if(isset($_POST['Submit']))
{
$start=$_POST['num1']; $end=$_POST['num2'];
echo "<table width='250' cellpadding='2' cellspacing='4' border='1'>";
echo "<tr><td>Number</td><td>Odd or Even?</td><td>Prime?</td></tr>";
for ($start;$start<=$end;$start++)
{
$answer=$start; $number=$answer; $check="text"; $num=$answer;
if($number%2==0)
{ $check="Even"; }
else
{ $check="Odd"; }
$prime = (isPrime($number)? 'Yes':'No');
echo "<tr><td>$answer</td><td>$check</td><td>$prime</td></tr>";
}
echo "</table>";
}
function isPrime($num) {
//1 is not prime. See: http://en.wikipedia.org/wiki/Prime_number#Primality_of_one
if($num == 1)
return false;
//2 is prime (the only even number that is prime)
if($num == 2)
return true;
/**
* if the number is divisible by two, then it's not prime and it's no longer
* needed to check other even numbers
*/
if($num % 2 == 0) {
return false;
}
/**
* Checks the odd numbers. If any of them is a factor, then it returns false.
* The sqrt can be an aproximation, hence just for the sake of
* security, one rounds it to the next highest integer value.
*/
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
if($num % $i == 0)
return false;
}
return true;
}
?>
try this
<?php
$x="num1";
$y="num2";
if(isset($_POST['Submit'])) {
$start=$_POST['num1'];
$end=$_POST['num2'];
echo "<table width='250' cellpadding='2' cellspacing='4' border='1'>";
echo "<tr><td>Number</td><td>Odd or Even?</td><td>Prime?</td></tr>";
for ($start;$start<=$end;$start++) {
$answer=$start;
$number=$answer;
$check="text";
$num=$answer;
if($number%2==0)
{
$check="Even";
}
else
{
$check="Odd";
}
if(isPrimeNumber($number))
{
$pirme_status = "Yes";
}
else
{
$pirme_status = "No";
}
echo "<tr><td>$answer</td><td>$check</td><td>$pirme_status</td><</tr>";
} echo "</table>";
}
and PHP function is
function isPrimeNumber($number)
{
$flag = true;
$max_count = ceil($number/2);
for($i=2; $i<$max_count; $i++)
{
if($number%$i==0)
{
$flag = false;
break;
}
}
return $flag;
}
?>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
So my taslk is to program two numbers randomly (for example 0 and 1) and then replace the numbers with "Hello" and "bye". I've done the generating it rendomly part, but now I'm struggling with the replacing Part. It would be cool If the solution would be by using "if" and "else"
Here's what i've done so far,
thank you in advance
<?php
$zaehler = 0;
while($zaehler < 10)
{
echo mt_rand(0, 1);
if(0)
{
echo "bye" ;
}
else (1) ;
{
echo "hello" ;
}
$zaehler++;
}
?>
For your case, just use a comparison statement
e.g.
if($result==0) { // do something;} else {// do another thing; }
(the $result will only be either 0 or 1, so just use one if-then-else)
<?php
$zaehler = 0;
while($zaehler < 10){
$result=mt_rand(0, 1);
echo $result;
if($result==0) {
echo "bye" ;
} else {
echo "hello" ;
}
$zaehler++;
}
?>
So i just saw your post,
First, you need to assign mt_rand to a variable.
Afer your statement IF isnt correct, you need to evaluate something like this $i === 0
And about your else you need an else if, so you can take a look to this code ;)
$idx = 0;
while ($idx < 10) {
$random_int = mt_rand(0, 1);
if ($random_int === 0) {
echo "bye\r\n";
} else if ($random_int === 1) {
echo "hello\r\n";
}
$idx++;
}
This question already has answers here:
PHP Displaying a number of asterisks depending on the number given in the input field
(4 answers)
Closed 11 months ago.
Question :
My Code :
<html\>
Enter the number of rows:
<?php
if($_POST)
{
$row = $_POST['row'];
if(!is_numeric($row))
{
echo "Make sure you enter a NUMBER";
return;
}
else
{
for($row=0;$row<=1;$row++){
for($j=0;$j<=$row;$j++){ echo "#"; } echo ""; }}}?>
The problem is it's showing only two rows
I expected as shown in the photo
$row = 10;
for($i=0;$i<=$row;$i++){
for($j=0;$j<=$i;$j++){ echo "#"; } echo "<br>"; }
You are overriding the $row variable.
Also, you don't need the else since you are returning in case the if(!is_numeric) is true;
This should do it.
if(isset($_POST['row'])) {
$rows = $_POST['row'];
if(!is_numeric($rows))
{
echo "Make sure you enter a NUMBER";
return;
}
for($row=0;$row<=$rows;$row++){
for($j=0;$j<=$row;$j++){
echo "#";
}
echo "\n";
}
}
You can make use of the PHP function str_repeat to simplify the script.
This would only take 1 loop, so you don't get confused with the variable names.
$row = 10;
$i = 1;
while ($i <= $row)
{
echo str_repeat("#", $i); echo "\n";
$i ++;
}
Working Example here.
I confused the method to flip the place of the echo result, below is my code
while ($looptools == 0) {
$mysqlihelper = " SELECT * FROM soal WHERE nomorsoal = $numberofmysqli ";
$mysqliquery = mysqli_query($konek, $mysqlihelper);
$resultquery = mysqli_fetch_assoc($mysqliquery);
$resulttextjudul = $resultquery['judul'];
if ($resulttextjudul == null) {
unset($resulttextjudul);
$resulttextjudul = "Tunggu Soal Berikutnya ! ..";
$nullerror = true;
} else {
}
if ($nullerror == true) {
echo "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">" . $resulttextjudul . "</div>";
} else {
echo "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">" . $resulttextjudul . "</div>";
}
if ($nullerror == true) {
mysqli_close($konek);
break;
} elseif ($looptools == 10) {
mysqli_close($konek);
break;
} else {
}
}
As you see in the "while", it's echo the first result and the second result below it, but I want the first result in below of the second result, can anyone tell me the method to do it?
I assume you mean you want to print all successes followed by all errors, or something like that. Here's how:
if($nullerror == true){
$errors .= "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>";
}else{
$successes .= "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>";
}
Then, when the while loop is done:
echo $successes ;
echo $errors ;
If you really want to work your way backwards through the results in $mysqliquery, that's a different answer.
UPDATE: to reverse the order of successes / errors on display, just put the latest additions in front:
if($nullerror == true){
$errors = "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>" . $errors;
}else{
$successes = "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>" . $successes ;
}
sorry if i answer my own question because i found it out myself
use mysqli_num_rows or often called mysqli count
http://php.net/manual/en/mysqli-result.num-rows.php
I have a query for one of my search function. A while loop is applied to this query. I have used many sets of if functions inside this while loop to further filter the results as per the user search requirement. And the {email id} (one of the variable in the step1 result) of the final filter result in the if statement set is used to query another table where in a foreach loop is used & obtain other specific details in that table.
// WHILE SEARCH PROFILES
while ($result = mysql_fetch_array($result_finda)) {
$emails=$result['email'];
$age=$result['age'];
$name=$result['name'];
$height=$result['height'];
$groups=$result['groups'];
$country=$result['country'];
if (($age_from <= $age)&& ($age <= $age_to)) {
$a_emails =$emails;
$a_age=$age;
$a_firstname=$name;
$a_height=$height;
$a_groups=$groups;
$a_country=$country;
}
if (($height_from <= $a_height)&& ($a_height <= $height_to)) {
$h_emails =$a_emails;
$h_age=$a_age;
$h_firstname=$a_firstname;
$h_groups=$a_groups;
$h_country=$a_country;
}
foreach ($_POST['groups'] as $workgroup) {
if (strpos($workgroup, $h_groups) !== false) {
$m_emails =$h_emails;
$m_age=$h_age;
$m_name=$h_name;
$m_groups=$h_groups;
$m_country=$h_country;
}
}
foreach ($_POST['country'] as $workcountry) {
if (strpos($workcountry, $m_country) !== false) {
$c_emails =$m_emails;
$c_age=$m_age;
$c_name=$m_name;
$c_groups=$m_groups;
$c_country=$m_country;
}
}
$findeducation="SELECT * FROM education WHERE email='$c_emails'";
$result_findeducation=mysql_query($findeducation);
$educationstatus = mysql_fetch_array($result_findeducation);
$profile_ed=$educationstatus['education'];
foreach ($_POST['education'] as $educationstatus) {
if (strpos($educationstatus, $profile_ed) !== false) {
$e_emails =$_c_emails;
$e_age=$c_age;
$e_name=$c_name;
$e_groups=$c_groups;
$e_country=$c_country;
$e_education=$profile_ed;
}
}
if ($e_name) {
$count++;
}
echo $e_name;echo ' ';
echo $e_emails;echo ' ';
echo $e_age;echo '</br>';
echo $e_groups;echo '</br>';
echo $e_country;echo '</br>';
}
I'm getting the desired result and The results are filtered and shown as per the code above.
My problem is that I'm not getting the total COUNT of the results displayed. Kindly advise for a solution.
Lot of thanks in advance.
SQL :
$count = mysql_num_rows($result_finda);
PHP :
$count = count(mysql_fetch_array($result_finda));
EDIT
Use the following :
$count = 0; // Initialize $count
while ($result = mysql_fetch_array($result_finda))
{
// Filter your results
$count++; // Increment $count for each result that matches with your filters
}
print $count; // number of results after filtering
In your code :
$finda="SELECT * FROM USERS WHERE gender='$gender'";
$result_finda=mysql_query($finda);
$count = 0;
while ($result = mysql_fetch_array($result_finda)) {
$emails=$result['email'];
$age=$result['age'];
$name=$result['name'];
$height=$result['height'];
$groups=$result['groups'];
$country=$result['country'];
if (($age_from <= $age)&& ($age <= $age_to)) {
$a_emails =$emails;
$a_age=$age;
$a_firstname=$name;
$a_height=$height;
$a_groups=$groups;
$a_country=$country;
}
if (($height_from <= $a_height)&& ($a_height <= $height_to)) {
$h_emails =$a_emails;
$h_age=$a_age;
$h_firstname=$a_firstname;
$h_groups=$a_groups;
$h_country=$a_country;
}
foreach ($_POST['groups'] as $workgroup) {
if (strpos($workgroup, $h_groups) !== false) {
$m_emails =$h_emails;
$m_age=$h_age;
$m_name=$h_name;
$m_groups=$h_groups;
$m_country=$h_country;
}
}
foreach ($_POST['country'] as $workcountry) {
if (strpos($workcountry, $m_country) !== false) {
$c_emails =$m_emails;
$c_age=$m_age;
$c_name=$m_name;
$c_groups=$m_groups;
$c_country=$m_country;
}
}
$findeducation="SELECT * FROM education WHERE email='$c_emails'";
$result_findeducation=mysql_query($findeducation);
$educationstatus = mysql_fetch_array($result_findeducation);
$profile_ed=$educationstatus['education'];
foreach ($_POST['education'] as $educationstatus) {
if (strpos($educationstatus, $profile_ed) !== false) {
$e_emails =$_c_emails;
$e_age=$c_age;
$e_name=$c_name;
$e_groups=$c_groups;
$e_country=$c_country;
$e_education=$profile_ed;
}
}
if ($e_name) {
$count++;
}
echo $e_name;echo ' ';
echo $e_emails;echo ' ';
echo $e_age;echo '</br>';
echo $e_groups;echo '</br>';
echo $e_country;echo '</br>';
}
print "$count RESULTS"; // [number] RESULTS
I have one more question for the day. I'm trying to make my biography page totally customizable for my custom CMS project I'm doing. If you notice in the view I have 3 h2 tags for Quotes, Allies, Rivals. What I would like to do is put the h3's into my db and then have it do a foreach loop for each one of them so I'm thinking some how I"m going to have to store the function that goes with the pageheading that way it doesn't have to run it if its not active on the page. I know this can be easily done however there's too much for me to focus on what I need to do in order to complete this. Keep in mind that depending on which page you are in the bio will impact which headings will be available.
As of right now here is my controller:
$activeTemplate = $this->sitemodel->getTemplate();
$footerLinks = $this->sitemodel->getFooterNav();
$bodyContent = "bio";//which view file
$bodyType = "main";//type of template
$this->data['activeTemplate'] = $activeTemplate;
$this->data['footerLinks']= $footerLinks;
$this->load->model('biomodel');
if($character !== "jfkdlsjl")
{
if((!empty($character))||(!isset($character))||(trim($character) !== '')||($character !== NULL))
{
$bioArray = $this->biomodel->getCharacterBio($character);
if ($bioArray == "empty")
{
$this->data['bioArray']= array();
}
else
{
if (($bioArray[0]->characters_statuses_id == 2)||($bioArray[0]->characters_statuses_id == 3)||($bioArray[0]->characters_statuses_id == 5))
{
$this->data['bioArray']= array();
}
else
{
$this->data['bioArray']= $bioArray;
$bioPagesArray = $this->biomodel->getBioPages();
$alliesArray = $this->biomodel->getCharacterAllies($bioArray[0]->id);
$rivalsArray = $this->biomodel->getCharacterRivals($bioArray[0]->id);
$quotesArray = $this->biomodel->getCharacterQuotes($bioArray[0]->id);
$this->data['bioPagesArray']= $bioPagesArray;
$this->data['alliesArray']= $alliesArray;
$this->data['rivalsArray']= $rivalsArray;
$this->data['quotesArray']= $quotesArray;
}
}
}
}
And here is my view:
echo "<h2>Quotes</h2>";
if (!empty($quotesArray))
{
echo "<ul>";
for($x = 0; $x <= (count($quotesArray)-1); $x++)
{
echo "<li>".stripslashes($quotesArray[$x]->quote)."</li>";
}
echo "</ul>";
}
echo "<h2>Allies</h2>";
if (!empty($alliesArray))
{
echo "<ul>";
foreach ($alliesArray as $row)
{
echo "<li>".stripslashes($row)."</li>";
}
echo "</ul>";
}
echo "<h2>Rivals</h2>";
if (!empty($rivalsArray))
{
echo "<ul>";
foreach ($rivalsArray as $row)
{
echo "<li>".stripslashes($row)."</li>";
}
echo "</ul>";
}
I don't know what you mean about storing the function nor which function you don't want to run.
Assuming we're working with the last else statement in your controller
$alliesArray = $this->biomodel->getCharacterAllies($bioArray[0]->id);
$rivalsArray = $this->biomodel->getCharacterRivals($bioArray[0]->id);
$quotesArray = $this->biomodel->getCharacterQuotes($bioArray[0]->id);
... and the function you "don't want to run" is the foreach loop on the array in the view, just handle the logic in your view:
if(($this->uri->segment(n)=='pageIwantQuotesOn') && (!empty($quotesArray)){
echo "<h2>Quotes</h2>";
echo "<ul>";
for($x = 0; $x <= (count($quotesArray)-1); $x++)
{
echo "<li>".stripslashes($quotesArray[$x]->quote)."</li>";
}
echo "</ul>";
}
...