Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This preform is very helpful for programmers. The community is very helpful.
I generally write programs in c# language, but I don't know php at advanced level.
In a project I need to write some code in PHP.
please help me...
This example will help me to learn php

C#
protected void Page_Load(object sender, EventArgs e)
    {

    }
    [System.Web.Services.WebMethod()]
    public static string getServerTime()
    {
        DateTime dt = DateTime.Now;
        return "Server_Time: " + dt.ToString();
    }
    [System.Web.Services.WebMethod]
    public static string get_mobinfo(string mobileno, string deviceid)
    {
        string tData;
        string remark_dl = ""; ;
        connect.connect_class cnn = new connect.connect_class();
        System.Data.DataTable dt = new System.Data.DataTable();
       
        string getsql = "SELECT free_join.Associate_Name, free_join.Mobile_No FROM free_join WHERE free_join.Mobile_No =  '" + mobileno + "'";
        dt = cnn.selctqrydtbl(getsql);

        // convert datatable to json using 
        tData = cnn.DataTable_2_JSON(dt);
        if (tData.Length > 2)
        {
            remark_dl = "Geting mobileinfo :" + mobileno + " :" + tData;
        }
        else
        {
            remark_dl = "Geting mobileinfo :" + mobileno + ":Not found";
        }

        
        string strSessionID = HttpContext.Current.Session.SessionID;
        string current_trackingid = cnn.create_trackingid(strSessionID);

       
        return tData;
    }

    [System.Web.Services.WebMethod]
    public static string get_shopdata(string mobile, string category)
    {
        string tData;
        string getsql;
        connect.connect_class cnn = new connect.connect_class();
        System.Data.DataTable dt = new System.Data.DataTable();
        if (category == "cat_toy")
        {
            getsql = "SELECT shop_product.slno AS SL_No, shop_product.productid AS Product_ID, shop_product.productname AS Product_Name, shop_product.price_mrp AS MRP, shop_product.price_offer AS Our_Price, shop_product.cat AS Category, shop_product.`status` AS Status FROM shop_product  WHERE status ='Active'";
            
        }
        else
        {
            getsql = "SELECT * from shop_product";
        }

        dt = cnn.selctqrydtbl(getsql);
        tData = cnn.DataTable_2_JSON(dt);

        Newtonsoft.Json.Linq.JObject product_obj = new Newtonsoft.Json.Linq.JObject();
        product_obj["ProductArray"] = tData;

        string product_json = product_obj.ToString();
       
        string getsql_cat = "SELECT * FROM shop_tag WHERE shop_tag.`status` = 'Active'";

        dt = null;
        dt = cnn.selctqrydtbl(getsql_cat);
        string tData_cat = cnn.DataTable_2_JSON(dt);

        Newtonsoft.Json.Linq.JObject cat_obj = new Newtonsoft.Json.Linq.JObject();
        cat_obj["CatArray"] = tData_cat;

        string cat_json = cat_obj.ToString();

        product_obj.Merge(cat_obj);

        string getsql_wish = "SELECT * FROM wishlist WHERE wishlist.`status` =  'A' AND wishlist.usermobile = '" + mobile + "' ORDER BY wishlist.slno ASC";

        dt = null;
        dt = cnn.selctqrydtbl(getsql_wish);
        string tData_wishlist = cnn.DataTable_2_JSON(dt);

        Newtonsoft.Json.Linq.JObject wish_obj = new Newtonsoft.Json.Linq.JObject();
        wish_obj["WishArray"] = tData_wishlist;

        product_obj.Merge(wish_obj);

        string retrnjson = product_obj.ToString();
        return retrnjson;
    }
    [System.Web.Services.WebMethod]
    public static string save_wishlist(string mobile, string fpid)
    {
        string tData;
        string getsql;
        connect.connect_class cnn = new connect.connect_class();
        System.Data.DataTable dt = new System.Data.DataTable();

        string jdat = null;
        string dtformat = "dd-MM-yyyy HH:mm:ss";

        DateTime dtime = cnn.localtime();
        jdat = dtime.ToString(dtformat);

        string insrtlogin = "insert into wishlist(usermobile,productid,doj,status) values('" + mobile + "','" + fpid + "','" + jdat + "','A')";
        cnn.selctqrydtbl(insrtlogin);       

        return "datasave";
    }
    public int GET_MAX_ID(string tbl_name, string fld_name)
    {
        int MAX_slno = 0;
        MySql.Data.MySqlClient.MySqlConnection mycn = new MySql.Data.MySqlClient.MySqlConnection();
        connect.connect_class cnn = new connect.connect_class();
        if (mycn.State == System.Data.ConnectionState.Open)
        {
            mycn.Dispose();
            mycn.Close();
        }
        mycn = cnn.myconect();

        string sql = "select max(" + fld_name + ") from " + tbl_name + "";
        MySql.Data.MySqlClient.MySqlDataAdapter cmd1 = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, mycn);

        System.Data.DataTable dt = new System.Data.DataTable();
        cmd1.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            string tmxid = dt.Rows[0][0].ToString();
            if (tmxid == "")
            {
                MAX_slno = 1;

            }
            else
            {
                MAX_slno = int.Parse(tmxid) + 1;
            }
        }
        return MAX_slno;       
    }


What I have tried:

sorry I don't know how to change this code
Posted
Updated 17-Mar-24 4:05am
v3

We are not a "C# to PHP" translator.

I'd suggest to read this: How to begin? | C# to PHP converter[^]
 
Share this answer
 
Comments
0x01AA 17-Mar-24 10:35am    
My 5
Maciej Los 17-Mar-24 13:01pm    
Thank you, Bruno :)
As Maciej has said, this is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.

So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it'll be a nightmare to debug if it doesn’t work "straight out of the box".

Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run. Since you wrote the C# code, you know exactly how it works, and that should make it fairly easy to write a new app to perform the same functions in a new framework.
 
Share this answer
 
Comments
Maciej Los 17-Mar-24 6:05am    
:)
As I am all about PHP, I found this interesting to play around with, be forewarned though that there are WAY more to the below code than what is given, you have to insert certain security measures, you need to know how to start a session to grab the input data to search your database with, you need to understand PHP PDO (PHP Data Objects) and how to use the parameterized statements, you need to read up on how to sanitize your returned data and then so much more...

As a rough sketch, your PHP will look something like this -
PHP
<?php

//Totally unsecure to have your database connection values here, should help with testing to see why it is needed...
$host = 'your_host';
$dbname = 'your_database';
$username = 'your_username';
$password = 'your_password';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Error: " . $e->getMessage());
}

function getServerTime() {
    $dt = new DateTime();
    return "Server_Time: " . $dt->format('Y-m-d H:i:s');
}

function get_mobinfo($mobileno, $deviceid) {
    $remark_dl = "";
    $tData = "";
    $getsql = "SELECT free_join.Associate_Name, free_join.Mobile_No FROM free_join WHERE free_join.Mobile_No = ?";
    
    try {
        $stmt = $pdo->prepare($getsql);
        $stmt->execute([$mobileno]);
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($result) {
            $remark_dl = "Getting mobileinfo: $mobileno: " . json_encode($result);
            $tData = json_encode($result);
        } else {
            $remark_dl = "Getting mobileinfo: $mobileno: Not found";
        }
    } catch (PDOException $e) {
        die("Error: " . $e->getMessage());
    }

    $strSessionID = session_id();
    $current_trackingid = create_trackingid($strSessionID);

    return $tData;
}

function get_shopdata($mobile, $category) {
    $tData = "";
    $product_obj = array();
    
    if ($category == "cat_toy") {
        $getsql = "SELECT shop_product.slno AS SL_No, shop_product.productid AS Product_ID, shop_product.productname AS Product_Name, shop_product.price_mrp AS MRP, shop_product.price_offer AS Our_Price, shop_product.cat AS Category, shop_product.`status` AS Status FROM shop_product WHERE status ='Active'";
    } else {
        $getsql = "SELECT * from shop_product";
    }

    try {
        $stmt = $pdo->query($getsql);
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        $product_obj["ProductArray"] = $result;
        
        $getsql_cat = "SELECT * FROM shop_tag WHERE shop_tag.`status` = 'Active'";
        $stmt = $pdo->query($getsql_cat);
        $result_cat = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        $product_obj["CatArray"] = $result_cat;
        
        $getsql_wish = "SELECT * FROM wishlist WHERE wishlist.`status` = 'A' AND wishlist.usermobile = ? ORDER BY wishlist.slno ASC";
        $stmt = $pdo->prepare($getsql_wish);
        $stmt->execute([$mobile]);
        $result_wish = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        $product_obj["WishArray"] = $result_wish;
        
        $tData = json_encode($product_obj);
    } catch (PDOException $e) {
        die("Error: " . $e->getMessage());
    }

    return $tData;
}

function save_wishlist($mobile, $fpid) {
    $jdat = date("Y-m-d H:i:s");
    
    $insrtlogin = "INSERT INTO wishlist(usermobile, productid, doj, status) VALUES(?, ?, ?, 'A')";
    
    try {
        $stmt = $pdo->prepare($insrtlogin);
        $stmt->execute([$mobile, $fpid, $jdat]);
    } catch (PDOException $e) {
        die("Error: " . $e->getMessage());
    }

    return "datasave";
}

function GET_MAX_ID($tbl_name, $fld_name) {
    $MAX_slno = 0;
    $sql = "SELECT MAX($fld_name) FROM $tbl_name";
    
    try {
        $stmt = $pdo->query($sql);
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($result) {
            $tmxid = $result[0];
            if ($tmxid == "") {
                $MAX_slno = 1;
            } else {
                $MAX_slno = intval($tmxid) + 1;
            }
        }
    } catch (PDOException $e) {
        die("Error: " . $e->getMessage());
    }

    return $MAX_slno;
}

?>
 
Share this answer
 
Comments
Pete O'Hanlon 17-Mar-24 6:43am    
5 from me.
Andre Oosthuizen 17-Mar-24 6:51am    
Thanks Pete!, much appreciated.
Vivek Kumar 18-Mar-24 6:42am    
thanks! much appreciated. 💗💗💗💗💗
Andre Oosthuizen 18-Mar-24 12:36pm    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900