PHP , Sum Of Common Division 2 Numbers - php

I want to Sum of Common Division Between two number,
in below is my code , and output is maghsom moshtarak is=24612
but Goal is 12 only;
please help me,
<?php
$m=0;
$j=6;
$h=12;
function SumCommon($j,$h)
{
echo"maghsom moshtarak is=";
for($i=1;$i<=$h;$i++)
{
if(($j%$i==0)&&($h%$i==0))
{
Sum($i);
}
}
}
function Sum($i)
{
$i+=$i;
$m=$i;
echo $m;
}
SumCommon($j,$h);
?>

<?php
$m=0;
$j=6;
$h=12;
function SumCommon($j,$h)
{
echo "maghsom moshtarak is=";
for($i=1;$i<=$h;$i++)
{
if(($i%$j==0)&&($i%$h==0)) /* this is changed */
{
Sum($i);
}
}
}
function Sum($i)
{
/* no need to use $i+=$i */
$m=$i;
echo $m;
}
SumCommon($j,$h);
?>
** Refined Code **
<?php
$j=6;
$h=12;
function SumCommon($j,$h)
{
echo "maghsom moshtarak is=";
for($i=1;$i<=$h;$i++)
{
if(($i%$j==0)&&($i%$h==0)) /* this is changed */
{
echo $i;
}
}
}
SumCommon($j,$h);
?>

Related

If-else condition gives me errors in PHP

I have two websites, each with it's own domain name. On load I have execute a php file that is used on both sites and has the same functionality.
Here is the code of that file:
<?php
echo '1';
if ($_SESSION["template"]=="template1";)
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template1/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template1/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template1/c.php');
}
else if ($_SESSION["template"]=="template2";)
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template2/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template2/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template2/c.php');
}
else {
echo 'NO DATA';
}
echo '4';
?>
On each of the two sites I set a session variable but in the above code it doesn't seem to work as I expect it to.
Am i missing something?
remove semicolon from if() and else if() statement, also add brackets when you using nested if else because it makes someone to understand easier and looks better
<?php
echo '1';
if ($_SESSION["template"]=="template1")
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template1/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template1/b.php');
}
else if { ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template1/c.php');
}
}
else if ($_SESSION["template"]=="template2")
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template2/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template2/b.php');
}
else if ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template2/c.php');
}
}
else {
echo 'NO DATA';
}
echo '4';
?>
After reviewing your code I edited my answer. The below code will do exactly the same as your code but requires alot less code.
<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php');
} else {
echo 'NO DATA';
}
?>

Display current category name

Thanks in advance.
I am trying to display the name of the current category which I'm in. Nothing that I've been trying for 4 hours now has worked..
My code:
if($catList) {
$c = 0;
while($category = $catList->fetch(PDO::FETCH_ASSOC)){
$margintop = 38*$c;
if($category['id'] == $thiscategory) {
echo "<a href='".$link."&category=".$category['id']."'>
<div class='category_name'>".strip_tags($category['name']). "</div></a></div>";
} else {
echo "<a href='".$link."&category=".$category['id']."'>
<div class='category_name'>".strip_tags($category['name']). "</div></a></div>";
}
$c++;
}
} else {
echo "No categories to show.";
}
what I've tried to get the current category name, in a different area in the page:
if($category['id'] == $thiscategory) {
echo ".strip_tags($category[name])." ;
}
which gives a blank result.
You need:
Compare with $category (instead of $thiscategory), because you set in link &category=
You need to show some difference if value is selected
if($catList) {
$c = 0;
while($myCategory = $catList->fetch(PDO::FETCH_ASSOC)){
$margintop = 38*$c;
if($myCategory['id'] == $category) {
echo "<a href='".$link."&category=".$myCategory['id']."' class=\"active\">
<div class='category_name'><strong>".strip_tags($myCategory['name']). "</strong></div></a></div>";
} else {
echo "<a href='".$link."&category=".$myCategory['id']."'>
<div class='category_name'>".strip_tags($myCategory['name']). "</div></a></div>";
}
$c++;
}
} else {
echo "No categories to show.";
}
Hey you may try single_cat_title()
Source: https://developer.wordpress.org/reference/functions/single_cat_title/

PHP - Unable to get same number of </ul> as <ul>

I am unable to generate same number of </ul> as <ul> in the following code:
<?php
$array=array("item1","item2","item3");
for ($i=0;$i<count($array);$i++)
{
if($i<count($array)-1)
{
echo '<li><span>'.$array[$i].'</span><ul>';
}
else
{
echo '<li><span>'.$array[$i].'</span>';
}
}
for ($i=0;$i<count($array);$i++)
{
echo '</ul></li>';
}
?>
The code is generating uneven number of <li><ul> and </ul></li>. Please tell me where I am going wrong?
This is a minor error. In your second for statement change $i<count($array); to $i<count($array)-1;.
So your final code will be:
<?php
$array=array("item1","item2","item3");
for ($i=0;$i<count($array);$i++)
{
if($i<count($array)-1)
{
echo '<li><span>'.$array[$i].'</span><ul>';
}
else
{
echo '<li><span>'.$array[$i].'</span>';
}
}
for ($i=0;$i<count($array)-1;$i++)
{
echo '</ul></li>';
}
?>
With if($i<count($array)-1) you check if $i is smaller than the length - 1.
That check isn't performed in the second loop. So the first one actually performs as many steps as the second one but only writes in all but the last iteration.
Try This:
$array=array("item1","item2","item3");
echo '<ul>';
for ($i=0;$i<count($array);$i++)
{
echo '<li><span>'.$array[$i].'</span></li>';
}
echo '</ul>;

To display output from mysql using PHP (json encode)

I'm trying to code a page like facebook-view_posts_page where I need to show the result as POST 1..Comment 1.. Comment 2.. POST 2.. Comment 3
The output of my code is
POST 1
POST 2
comment 1
comment 2
comment 3
How should I re write my code?
<?php
include("connect.php");
$userID=$_REQUEST['userID'];
$Query=("select * from tb_post where userID='$userID'");
$result=mysql_query($Query);
$count=mysql_num_rows($result);
if($count>0)
{
//$post['result']="sucess";
$joinQuery=("select * from tb_post where tb_post.userID='$userID'");
$joinResult=mysql_query($joinQuery);
while($row=mysql_fetch_assoc($joinResult))
{
$posts[]=$row;
$postid=$row['postID'];
$commentQuery=("select tb_comment.commentID,tb_comment.userID ,tb_comment.postID ,tb_comment.comment ,tb_comment.date,signup.userName,signup.image from tb_comment,signup where tb_comment.postID='$postid' and signup.userID=tb_comment.userID");
$commentResult=mysql_query($commentQuery);
//$post['posts']=$posts;
while($commentrow=mysql_fetch_assoc($commentResult))
{
$comments[]=$commentrow;
}
}
$post=array("result"=>"success","posts"=>$posts,"comments"=>$comments);
}
else
{
$post['result']="failed";
$post['error']="no data found";
}
$data='content-type:application/json';
$data=json_encode($post);
echo $data;
?>
I will explain you the approach.
You are fetching and appending comments in an array without any direct key reference to the post.
array comments;
while (comment = fetch comments) {
comments[post id][] = comment;
}
while showing:
while (post = fetch posts) {
echo post title;
foreach (comments[post id] as postComment) {
echo postComment;
}
}
Comments should have reference to the post as key of comments array.
In your case, you are already fetching comments correctly, just change the following inner while loop to:
while($commentrow=mysql_fetch_assoc($commentResult)) {
$comments[$commentrow['postID'][]=$commentrow; // Observe postID
}
Fully working code:
<?php
include("connect.php");
$userID=$_REQUEST['userID'];
$Query=("select * from tb_post where userID='$userID'");
$result=mysql_query($Query);
$count=mysql_num_rows($result);
if($count>0) {
$joinQuery=("select * from tb_post where tb_post.userID='$userID'");
$joinResult=mysql_query($joinQuery);
while($row=mysql_fetch_assoc($joinResult)) {
$posts[]=$row;
$postid=$row['postID'];
$commentQuery=("select tb_comment.commentID,tb_comment.userID ,tb_comment.postID ,
tb_comment.comment ,tb_comment.date,signup.userName,signup.image
from tb_comment,signup
where tb_comment.postID='$postid' and signup.userID=tb_comment.userID");
$commentResult=mysql_query($commentQuery);
while($commentrow=mysql_fetch_assoc($commentResult)) {
$comments[$commentrow['postID']][] = $commentrow;
}
}
$post=array("result"=>"success","posts"=>$posts,"comments"=>$comments);
}
else {
$post['result']="failed";
$post['error']="no data found";
}
$data='content-type:application/json';
$data=json_encode($post);
echo $data;
?>
You can print posts and its comments like this:
<?php
if (! empty($posts)) {
foreach ($posts as $post) {
echo $post['post_title_field'] . "<br/>";
if (! empty($comments[$post['postID']])) {
foreach ($comments[$post['postID']] as $postComment) {
echo postComment . "<br/>";
}
}
}
}
?>
Thanks everyone for helping me.My friend came up with another solution, which is the code I'm looking for.And here is the code:
<?php
include("connect.php");
$sel_post=mysql_query("SELECT * FROM tb_post WHERE userID='".$_REQUEST['userID']."'");
if(mysql_num_rows($sel_post)>0)
{
while($row=mysql_fetch_assoc($sel_post))
{
$sel_post_owner=mysql_query("SELECT name,image FROM signup WHERE userID='".$row['userID']."'");
if(mysql_num_rows($sel_post_owner)>0)
{
while($row_owner=mysql_fetch_array($sel_post_owner))
{
$row['post_owner_name']=$row_owner['name'];
$row['post_owner_image']=$row_owner['image'];
}
}
else
{
$row['post_owner_name']="";
$row['post_owner_image']="";
}
$sel_comments=mysql_query("SELECT * FROM tb_comment WHERE postID='".$row['postID']."'");
if(mysql_num_rows($sel_comments)>0)
{
$comments=array();
while($row_comment=mysql_fetch_assoc($sel_comments))
{
$sel_comment_owner=mysql_query("SELECT name,image FROM signup WHERE userID='".$row_comment['userID']."'");
if(mysql_num_rows($sel_post_owner)>0)
{
while($row_comment_owner=mysql_fetch_array($sel_comment_owner))
{
$row_comment['comment_owner_name']=$row_comment_owner['name'];
$row_comment['comment_owner_image']=$row_comment_owner['image'];
}
}
else
{
$row_comment['comment_owner_name']="";
$row_comment['comment_owner_image']="";
}
$comments[]=$row_comment;
}
$row['comments']=$comments;
}
else
{
$row['comments']=array();
}
$Post[]=$row;
}
$post=array("result"=>"success","Posts"=>$Post);
}
else
{
$post['result']="failed";
$post['error']="no data found";
}
$data='content-type:application/json';
$data=json_encode($post);
echo $data;
?>

how to append numbers into string in php

I have a for loop like this in php
<?php
for($i=0;$i<=100;$i++)
{
echo $i;
}
What I need is, I want to generate roll number using this for loop like this.
SN0001
SN0002
SN0003
.....
SN0010 not SN00010
......
SN0100 not SN000100
I have tried this
<?php
for($i=0;$i<=100;$i++) {
$str='SN00'.$i;
echo $str;
}
?>
Note this (SN0010 and SN0100). I need SN0010,SN0100 not SN00010, SN000100.
What should I do for that?
You can use str_pad() function:
<?php
for($i=0;$i<=100;$i++) {
echo 'SN' . str_pad($i, 4, STR_PAD_LEFT, '0');
}
?>
Please try this code-
for($i=0;$i<=100;$i++) {
echo 'SN' .sprintf("%'.04d\n", $i);
}
for($i=0;$i<=100;$i++)
{
if($i<=9){
$zeroValue = '000';
}
elseif($i>9 && $i<=99){
$zeroValue = '00';
}
elseif($i>99 && $i<=999){
$zeroValue = '0';
}
echo 'SN'.$zeroValue.$i.'<br>';
}
<?php
for($i=0;$i<=100;$i++) {
$str='SN00'.$i;
if(strlen($str)<6)
{
$str='SN000'.$i;
}
if(strlen($str)>6)
{
$str='SN0'.$i;
}
echo $str;
}
?>

Categories