Saturday, 22 February 2020

Dynamic Combobox

Dear all

Using this code a combo box can be dynamically populated using php code.

-------------------------
<?php
if($con=mysql_connect("localhost","root",""))
{
//echo"connected";
}
else
{
//echo"not connected";
}
mysql_select_db("fjc");



//start combo box code.

function showcombobox($tablename,$valuefield,$showfield)
{

$sql="select $valuefield,$showfield from $tablename";
echo $sql;
$data=mysql_query($sql);
$retvalue='';
while($row=mysql_fetch_array($data))
{

$retvalue=$retvalue."<option value='".$row[$valuefield]."'>".$row[$showfield]."</option>";

}

return $retvalue;
}

//start combo box code.

?>
-------------------------------

Sunday, 9 February 2020

MVC in core PHP

Hi All,
This is a post dedicated for creating MVC program using core PHP

////View

<html>
<head>
<form action="controller.php" method=post>
<b>  NAME <input type="text" name="name">
ADDRESS <input type="text" name="address">
COURSE<input type="text" name="course"></b>
<input type="submit" name=btnsub value="OK">
</form>
</html>


/////Controller

<?php
include ('model.php');
 if(isset($_POST["btnsub"]))
 {
echo "entered";
 $con=mysql_connect("localhost","root","");
 mysql_select_db("mvc",$con);
 $s=new student();
 $name=$_POST['name'];
 $address=$_POST['address'];
 $course=$_POST['course'];
 $s->insert($name,$address,$course);
  //header('location: model.php');



 }
 ?>


/////Model

<?php
class student {
function __construct() {

}

function insert($name,$address,$course ) {
echo "insert worked";
$sql=("insert into student values('$name','$address','$course')");
mysql_query($sql);

}
}
?>

Monday, 27 January 2020

Showing videos which are stored in Server using PHP

Hi All,
This code can be used to view videos which are stored in server.
Table contents are shown using reusable code here.
You have to change only the sql in the below given code.
No need to type the entire code.
Just copy and run.


-----------------------------------------------------------------------------------------------------------
<center>
<?php
include('connect.php');

$query = 'select * from upload';
$result = mysql_query($query);
if (!$result)
{
$message = 'ERROR:' . mysql_error();
return $message;
}
else
{
$i = 0;
echo '<html><body><table border=1 align=center><tr>';
while ($i < mysql_num_fields($result))
{
$meta = mysql_fetch_field($result, $i);
echo '<th>' . ucfirst($meta->name) . '</th>';
$i = $i + 1;
}
echo '<th>View</th></tr>';
$i = 0;
while ($row = mysql_fetch_row($result))
{
echo '<tr>';
$count = count($row);
$y = 0;
$idval='1';
while ($y < $count)
{
$c_row = current($row);
if($y==0)
$idval=$c_row;
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '<td><a href=videoupload1/'.$idval.'.mp4>View Video</a></td>';
echo '</tr>';
$i = $i + 1;
}
echo '</table></body></html>';
mysql_free_result($result);
}
mysql_close ($con);

?></center>

Friday, 10 January 2020

Create a Hashtag with Auto Id

Hi All,
Using this code you can save data to a table with auto generated ID in PHP.

--------------------------
Table


CREATE TABLE IF NOT EXISTS `hashtag` (
  `hid` int(11) NOT NULL,
  `tag` text NOT NULL,
  `description` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


---------------------------

<html>
<body>
<?php

mysql_connect("localhost","root","");
mysql_select_db("tcsr");

$sql1="select max(hid) as mid from hashtag ";
$data1=mysql_query($sql1);

$id=0;

while($row=mysql_fetch_array($data1))
{


   $id=$row['mid'];
}
$id=$id+1;
echo $id;

?>
<form name="frm"   method=post enctype="multipart/form-data" >
<pre>
Id           <input type="text" value="<?php echo $id; ?>" disabled=disabled name="hid" /> <input  name="hid1" type="hidden" value="<?php echo $id; ?>" />
HashTag      <input type="text" name="hashtag" />
Description  <input type="text" name="description" />

<input name="hsubmit" type="submit" value="Upload files" />
</form>



<?php
echo "entered";
if(isset($_POST['hsubmit']))
{
echo "Entered";
$hid=$_POST['hid1'];
$hashtag=$_POST['hashtag'];
$desc=$_POST['description'];

$sql="insert into hashtag values('$hid','$hashtag','$desc')";
echo $sql;
$result=mysql_query($sql);

}

?>
</body>
</html>

Friday, 27 December 2019

Star Rating in PHP

Hi All,

See the code
---------------------------------------------

CREATE TABLE IF NOT EXISTS `upload` (
  `vid` int(10) NOT NULL,
  `uid` int(10) NOT NULL,
  `videos` varchar(50) NOT NULL,
  `details` varchar(50) NOT NULL,
  `date` date NOT NULL,
  `points` int(11) NOT NULL,
  PRIMARY KEY (`vid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

----------------------------------------------
viewvideos.php

<?php
include('connect.php');

$query = 'select * from upload';
$result = mysql_query($query);
if (!$result)
{
$message = 'ERROR:' . mysql_error();
return $message;
}
else
{
$i = 0;
echo '<html><body><table border=1 align=center><tr>';
while ($i < mysql_num_fields($result))
{
$meta = mysql_fetch_field($result, $i);
echo '<th>' . ucfirst($meta->name) . '</th>';
$i = $i + 1;
}
echo '<th>View</th>';
  echo '';
  echo '</tr>';
$i = 0;
while ($row = mysql_fetch_row($result))
{
echo '<tr>';
$count = count($row);
$y = 0;
$idval='1';
while ($y < $count)
{
$c_row = current($row);
if($y==0)
$idval=$c_row;
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '<td><a href=videoupload/'.$idval.'.mp4>View Video</a></td>';
    echo '<td><a href=rating.php?vid='.$idval.'&val=1><img width=30 height=30 src=images/star.jpg></a><a href=rating.php?vid='.$idval.'&val=2><img width=30 height=30 src=images/star.jpg></a><a href=rating.php?vid='.$idval.'&val=3><img width=30 height=30 src=images/star.jpg></a></td>';
echo '</tr>';
$i = $i + 1;
}
echo '</table></body></html>';
mysql_free_result($result);
}
mysql_close ($con);

?></center>

----------------------------------------------
rating.php
<?php
 include('connect.php');
 session_start();
 $id=$_GET['vid'];
 $val=$_GET['val'];
 $sql="update upload set points=points+".$val." where vid='$id'";
 echo $sql;
 mysql_query($sql,$con);
header('location:viewvideos.php')
?>

View data using PHP

HI All,

See the code

------------------------------------------------------------------------------

<?php
include('connect.php');

$query = 'select * from upload';
$result = mysql_query($query);
if (!$result)
{
$message = 'ERROR:' . mysql_error();
return $message;
}
else
{
$i = 0;
echo '<html><body><table border=1 align=center><tr>';
while ($i < mysql_num_fields($result))
{
$meta = mysql_fetch_field($result, $i);
echo '<th>' . ucfirst($meta->name) . '</th>';
$i = $i + 1;
}
echo '<th>View</th>';
  echo '<th>Rating</th></tr>';
$i = 0;
while ($row = mysql_fetch_row($result))
{
echo '<tr>';
$count = count($row);
$y = 0;
$idval='1';
while ($y < $count)
{
$c_row = current($row);
if($y==0)
$idval=$c_row;
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '<td><a href=videoupload/'.$idval.'.mp4>View Video</a></td>';
    echo '<td><a href=rating.php><img src="images/star.jpg"></a></td>';
echo '</tr>';
$i = $i + 1;
}
echo '</table></body></html>';
mysql_free_result($result);
}
mysql_close ($con);

?></center>
<?php

//Showing image in the table
/*$query = 'select * from userreg';
$result = mysql_query($query);
if (!$result)
{
 $message = 'ERROR:' . mysql_error();
 return $message;
}
else
{
 $i = 0;
 echo '<html><body><table border=1 align=center><tr>';
 while ($i < mysql_num_fields($result))
 {
  $meta = mysql_fetch_field($result, $i);
  echo '<th>' . ucfirst($meta->name) . '</th>';
  $i = $i + 1;
 }
 echo '<th>Delete</th></tr>';
 $i = 0;
 while ($row = mysql_fetch_row($result))
 {
  echo '<tr>';
  $count = count($row);
  $y = 0;
  $idval='1';
  while ($y < $count)
  {
   $c_row = current($row);
   if($y==0)
    $idval=$c_row;
   echo '<td>' . $c_row . '</td>';
   next($row);
   $y = $y + 1;
  }
  echo "<td><img width=30 height=30 src='uploads/

$idval.jpg'></td><td><a href=delComplaint.php?id='.

$idval.'>Delete</a></td>";
  echo '</tr>';
  $i = $i + 1;
 }
 echo '</table></body></html>';
 mysql_free_result($result);
}
mysql_close ($con);*/

?>

Wednesday, 13 November 2019

Complete Prediction code

Hi All
Follow these four steps
DB Name :predictdb
Table Name  : predictdata



CREATE TABLE IF NOT EXISTS `predictdata` (
  `pid` int(11) NOT NULL,
  `temperature` text NOT NULL,
  `pressure` text NOT NULL,
  `sugar` text NOT NULL,
  `totalcount` text NOT NULL,
  `inputstring` text NOT NULL,
  `predictionresult` text NOT NULL
)


1. Input form and DB save with input string generation
----------------------------------------------------------------------------------------------------------

<html>
<body>
<form method=post >
<pre>
Patient Id  <input name=pid>
Temperature <select name=temperature>
            <option value='vhigh'>Very High</option>
            <option value='high'>High</option>
            <option value='medium'>Medium</option>
            <option value='low'>Low</option>
            <option value='vlow'>Very Low</option>
            </select>
Pressure <select name=pressure>
            <option value='vhigh'>Very High</option>
            <option value='high'>High</option>
            <option value='medium'>Medium</option>
            <option value='low'>Low</option>
            <option value='vlow'>Very Low</option>
            </select>
Sugar    <select name=sugar>
            <option value='vhigh'>Very High</option>
            <option value='high'>High</option>
            <option value='medium'>Medium</option>
            <option value='low'>Low</option>
            <option value='vlow'>Very Low</option>
            </select>
Totalcount <input name=count>
<input type=submit name='btnsave' value='Save'>
</form>
<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("predictdb",$con);

if(isset($_POST['btnsave']))
{
$totalcount='';
if($_POST['count']<=100)
$totalcount='low';
else
$totalcount='high';
$inputstring="t_".$_POST['temperature']." p_".$_POST['pressure']." s_".$_POST['sugar']." tc_".$totalcount;
$qr="insert into predictdata values('$_POST[pid]','$_POST[temperature]','$_POST[pressure]','$_POST[sugar]','$totalcount','$inputstring','')";
echo $qr;
mysql_query($qr,$con);

}


?>

</body>
</html>


----------------------------------------------------------------------------------------------------------

2. Download testdata.csv
----------------------------------------------------------------------------------------------------------


<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("predictdb",$con);
header('Content-Type: text/csv; charset=utf-8');
      header('Content-Disposition: attachment; filename=da.csv');
       $output = fopen("php://output", "w");
     fputcsv($output, array('pid','inputstring'));
         $query = "select *  from predictdata";
         $result = mysql_query($query, $con);
         while($row = mysql_fetch_array($result))
         {
              fputcsv($output,array($row['pid'],$row['inputstring']));
             
         }
      fclose($output);

?>
----------------------------------------------------------------------------------------------------------


3. Predict using python
----------------------------------------------------------------------------------------------------------
from nltk import NaiveBayesClassifier as nbc
from nltk.tokenize import word_tokenize
from itertools import chain
import csv

import nltk
nltk.download('punkt_tab')

with open('trainingdata.csv','r'as csvinput:
    reader=csv.reader(csvinput,delimiter=",")
    rownum = 0 
    training_data = []

    for row in reader:
        training_data.append (row)
        rownum += 1

vocabulary = set(chain(*[word_tokenize(i[0].lower()) for i in training_data]))

feature_set = [({i:(i in word_tokenize(sentence.lower())) for i in vocabulary},tag) for sentence, tag in training_data]

classifier = nbc.train(feature_set)

with open('testdata.csv','r'as csvinput:
    with open('data.csv''w'as csvoutput:
        writer = csv.writer(csvoutput, lineterminator='\n')
        reader1 = csv.reader(csvinput)

        all = []
        #row = next(reader1)
        

        for row in reader1:
            test_sentence = row[1]
            featurized_test_sentence =  {i:(i in word_tokenize(test_sentence.lower())) for i in vocabulary}
            print ("test_sent:",test_sentence)
            print ("tag:",classifier.classify(featurized_test_sentence))
            row.append(classifier.classify(featurized_test_sentence))
            all.append(row)
        writer.writerows(all)


----------------------------------------------------------------------------------------------------------


4. Upload result to web using data.csv
----------------------------------------------------------------------------------------------------------
<form method='POST' enctype='multipart/form-data'>
<center>
Upload CSV FILE: <input type='file' name='csv_info' />
<input type='submit' name='submit' value='Upload' style="height: 50px; width: 150px; left: 250; top: 250;"/>
</center>
</form>
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("predictdb",$con);
if(isset($_POST['submit'])){
if($_FILES['csv_info']['name']){
  $arrFileName = explode('.',$_FILES['csv_info']['name']);
   if($arrFileName[1] == 'csv'){
     $handle = fopen($_FILES['csv_info']['tmp_name'], "r");
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
         $id=$data[0];
         $result = $data[2];
     
         $sql="update predictdata set predictionresult='$result' where pid='$id'";
mysql_query($sql);
                                   }
                        fclose($handle);
}
}
}
?>


----------------------------------------------------------------------------------------------------------