I have made a voice dictionary. The user inputs a word and the corresponding meaning is fetched from my sql database , it is displayed on screen and then converted to speech.Its working all fine.
I just want to add one condition that : If user enters a word that is not present in my database then it outputs the message " sorry word not found", and nothing should be played in the audio.
Currently if i enter a word which is not present , i am getting the audio of 'undefined index..something.. '
plz tell me where to add the if condition and what to add
Here is my code
<html>
<head>
<title>Word meanings</title>
<?php
mysql_connect("localhost", "root", "abcd");
mysql_select_db("dictionary");
if(isset($_POST['Submit1']))
{
$req=$_REQUEST['word'];
$strSQL = "SELECT * FROM dict WHERE word='$req'";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs))
{
$x=$row["word"];
$y=$row["meaning"];
$z=$row["synonym"];
echo "<b>Word</b>: " . $x ."<br/>" ;
echo "<b>Meaning</b>: " . $y ."<br/>" ;
echo "<b>Synonym</b>: " . $z ."<br/>" ;
}
}
mysql_close();
?>
</head>
<body>
<form name="form1" action="lastry.php" method="POST">
<input type="text" name="word" value="<?php echo isset($_POST['word'])?$_POST['word']:''?>"x-webkit-speech/>
<Input Type ="Submit" Name ="Submit1" Value ="submit">
</form>
<?php
if($_POST)
{
?>
<p>Listen word
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($x);?>'" &type="audio/mp3" />
</audio>
</p>
<br><br>
<p>Listen meaning
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($y);?>" &type="audio/mp3" />
</audio>
</p>
<br><br>
<p>Listen synonym
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($z);?>'" &type="audio/mp3" />
</audio>
</p>
<?php
}
?>
</body>
</html>
Immediately after $rs = mysql_query($strSQL);, add the line $num_rows = mysql_num_rows($rs);, then you can have:
if($num_rows > 0) {
/*Run while loop which you already have*/
}
else {
echo 'Sorry word not found';
}
maybe check if it has value in it
if( isset($_POST) && !empty($x) )
Add counter to here:
$foundwords = 0;
while($row = mysql_fetch_array($rs))
{
$x=$row["word"];
$y=$row["meaning"];
$z=$row["synonym"];
echo "<b>Word</b>: " . $x ."<br/>" ;
echo "<b>Meaning</b>: " . $y ."<br/>" ;
echo "<b>Synonym</b>: " . $z ."<br/>" ;
$foundwords++; // increase counter after each found word
}
and then later after mysql_close() check that variable (somewhere inside the body)
if($foundwords==0) {
echo 'sorry word not found';
... finish html etc ...
die();
}
you should do something like this :
<?php
if (isset($_POST['Submit1']) {
mysql_connect("localhost", "root", "abcd");
mysql_select_db("dictionary");
$req=$_REQUEST['word'];
$rs = mysql_query("SELECT * FROM dict WHERE word='$req'");
while($row = mysql_fetch_array($rs)){
$x=$row["word"];
$y=$row["meaning"];
$z=$row["synonym"];
echo "<b>Word</b>: " . $x ."<br/>" ;
echo "<b>Meaning</b>: " . $y ."<br/>" ;
echo "<b>Synonym</b>: " . $z ."<br/>" ;
}
if (!isset($x)) { $All_Find = $req; } else { $All_Find = true; }
}
?>
<?php if ($All_Find == true) { ?>
<audio controls="controls">
<source src="http://speechutil.com/convert/ogg?text='<?php echo urlencode($y);?>" &type="audio/mp3" />
</audio>
<?php } else { echo "Sorry, the world $All_Find cannot be found :(";}?>
The undefined index message is mainly the result of your php.ini settings. If you turn of Notice are Warning error levels there. The value of a no existing associative array member in php is always NULL ( = false = '' ).
If you want to be exact however, you could check like this
// the condition evaluates to false if $row has no 'meaning' member
if($row['meaning'])
$z = $row['meaning'];
else
$z = "sorry word not found";
Related
When I enter a value it should be numeric only. In my code the check seems to be false because I can enter several non numeric items.
Do you have an idea ?
<body>
<form method="GET" action="">
<input type="text" name="niveau">
<input type="submit" value="valider" >
</form>
<br />
<?php
if (empty($_GET['niveau']))
{
echo 'Enter niveau please : ' . "</br>";
}
else if(isset($_GET['niveau']))
{
echo "Niveau : " .$_GET['niveau'] . "</br>";
}
else if (is_numeric($_GET['niveau']))
{
echo "Niveau is numeric" ;
}
else
{
echo "$var_name1 is not numeric. <br>" ;
}
?>
</body>
The problem is your if/else if structure. The first else if checks whether or not the niveau parameter is set. If yes you print out the niveau level and the second else if condition won't be checked.
You could for example include the second else if condition within the first one, something like this:
if (empty($_GET['niveau']))
{
echo 'Enter niveau please : ' . "</br>";
}
else if(isset($_GET['niveau']) && is_numeric($_GET['niveau']))
{
echo "Niveau : " .$_GET['niveau'] . "</br>";
}
else
{
echo "$var_name1 not set or not numeric. <br>" ;
}
I can't access array from other file and still don't figure it out, whether my data already stored into array or not. Should i put $_SESSION into the function?
Lat3_3a.php
<form id="form1" name="form1" method="post" action="Lat3_3b.php">
Insert number: <input type="number" name="num" id="num" />
<input type="submit" name="button" id="button" value="OK" />
</form>
Lat3_3b.php
<?php
session_start();
$_SESSION["num"] = $_POST["num"];
if (empty($_SESSION["num"]))
echo "Please, insert number";
else {
$val=$_POST['num'];
echo " Factorial " .$val. " ! = " .factorial($val)."<br/>";
echo "<a href='Lat3_3c.php'>Link</a>";
}
function factorial($val){
if($val<=1){
$result=1;
return $result;
}elseif($val>1){
for($i=1; $i<=$val; $i++){
$result=$val * factorial($val-1);
}
return $result;
}
$data=array($val,$result,"12345", "Travis");
$_SESSION["var"]=$_POST["data"];
}
?>
Lat3_3c.php
<?php
session_start();
if(empty($_SESSION["var"]))
echo "Variable not found";
else
echo "Data : ". $_SESSION["var"];
?>
Ok looking a bit more in to your code, you need to learn more about the session, post and functions. For that a look at the PHP manual site.
Site http://php.net
For your code to work you need to use something like this:
<?php
session_start();
$_SESSION["num"] = $_POST["num"];
$_SESSION["var"]=array();
if (empty($_SESSION["num"])){
echo "Please, insert number";
}
else {
$val=$_POST['num'];
$function_result=factorial($val);
$data=array($val,$function_result,"12345","Travis");
$_SESSION["var"]=$data;
echo " Factorial " .$val. " ! = " .$function_result."<br/>";
echo "<a href='Lat3_3c.php'>Link</a>";
}
function factorial($val){
if($val<=1){
$result=1;
return $result;
}elseif($val>1){
for($i=1; $i<=$val; $i++){
$result=$val * factorial($val-1);
}
return $result;
}
}
?>
I'm not looking into your math.
I am making a voice dictionary. Here is the code . Its working fine except for problem that text to speech is working for some specific texts only.
Eg if the string returned to the variable $y="hello world" then its being converted to audio
but if its "world hello" then there is no audio output.
plz help. thanks
<html>
<head>
<title>Word meanings</title>
<?php
mysql_connect("localhost", "root", "abcd");
mysql_select_db("dictionary");
if(isset($_POST['Submit1']))
{
$req=$_REQUEST['word'];
$strSQL = "SELECT * FROM dict WHERE word='$req'";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs))
{
$x=$row["word"];
$y=$row["meaning"];
$z=$row["synonym"];
echo "<b>Word</b>: " . $x ."<br/>" ;
echo "<b>Meaning</b>: " . $y ."<br/>" ;
echo "<b>Synonym</b>: " . $z ."<br/>" ;
}
}
mysql_close();
?>
</head>
<body>
<form name="form1" action="lastry.php" method="POST">
<input type="text" name="word" value="<?php echo isset($_POST['word'])?$_POST['word']:''?>"x-webkit-speech/>
<Input Type ="Submit" Name ="Submit1" Value ="submit">
</form>
<?php if($_POST)
{
?>
<audio controls="controls" autoplay="autoplay">
<source src="http://tts-api.com/tts.mp3?q=<?php echo urlencode($y);?>&type="audio/mp3" />
</audio>
<?php }?>
</body>
</html>
I think problem in API, which you use, go to
http://tts-api.com/
and put "word hello", you'll get redirect to
http://tts-api.com/tts.mp3?q=word%20hello
where you can see only 500.
So, you need contact API support.
i'm new in programming and have been working on a searchable database which can retrieve images by typing in keywords and after pressing submit will show results and the picture.
But so far i have no luck in getting the picture to show(broken link/image) but my search form does work and does correctly retrieve the name or result.
My table in phpmyadmin name is shoes , and i have 3 columns, 1 id (int15 PRI) ,2 brand/model (varchar 50), 3 picture (longblob).
My code is relative simple and hope you can help me out =)
File name: search.php
<form action="search.php" method="POST">
Name: <input type ="text" name="search_name"> <input type="submit" value="Search">
<?php
if (isset($_POST['search_name'])) {
$search_name = $_POST['search_name'];
if (!empty($search_name)){
if (strlen($search_name)>=3) {
$query = "SELECT * FROM `shoes` WHERE `brand/model` LIKE '%".mysql_real_escape_string($search_name)."%'";
$query_run = mysql_query($query);
$query_num_rows = mysql_num_rows($query_run);
if ($query_num_rows>=1) {
echo $query_num_rows.' Results found:<br>';
while ($query_row = mysql_fetch_array($query_run)) {
$picture = $query_row['picture'];
echo "</br>";
echo $query_row ['brand/model'];
echo "</br>";
echo "</br>";
//header("content-type: image/jpeg");
echo "<img src=image.php?id=".$row['id']." width=300 height=200/>";
echo "</br>";
}
} else {
echo 'No Results Found.';
}
} else {
echo 'Text field must be more than 3 characters.';
}
} else {
echo 'Text Field Cannot be Empty!';
}
}
?>
i have a image.php here
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$conn = mysql_connect("localhost","root","");
if(!$conn){
echo mysql_error();
}
$db = mysql_select_db("phsdatabase");
if(!$db){
echo mysql_error();
}
$id = $_GET['id'];
$query = "SELECT `picture` FROM shoes where id='$id'";
$query_run = mysql_query("$query",$conn);
if($query_run){
$row = mysql_fetch_array($query_run);
$type = "Content-type: image/jpeg";
header($type);
echo $row['picture'];
} else {
echo mysql_error();
}
?>
storeinfo.php to store new info,
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$conn = mysql_connect("localhost","root","");
if(!$conn)
{
echo mysql_error();
}
$db = mysql_select_db("phsdatabase",$conn);
if(!$db)
{
echo mysql_error();
}
#$brandmodel = $_POST['brand/model'];
#$picture = addslashes (file_get_contents($_FILES['picture']['tmp_name']));
#$image = getimagesize($_FILES['picture']['tmp_name']);//to know about image type etc
//$imgtype = $image['mime'];
if (isset($_POST['brand/model'])){
$brandmodelentry = $_POST['brand/model'];
if (!empty($brandmodelentry)){
if (strlen($brandmodelentry)>=3) {
$query ="INSERT INTO shoes VALUES('','$brandmodel','$picture')";
$query_run = mysql_query($query,$conn);
echo '<br>';
echo "Information Stored Successfully!";
} else {
echo mysql_error();
}
echo '<br>';
echo '<br>';
echo "Thank you for Registering new information to our database!";
} else{
echo 'Text Field cannot be empty!';
}
}
?>
newentry.php which register new info
<form enctype="multipart/form-data" action="storeinfo.php" method="POST">
<center>Shoes Information</center>
Brand and Model Name<input type=text name="brand/model">
Picture of Shoes(Acceptable formats:<br>JPEG,JPG,PNG)<input type="file" name="picture" id ="picture">
<input type=submit name="submit" value="Store Information">
Your code is absolutely correct except single line i.e.
echo "<img src=image.php?id=".$row['id']." width=300 height=200/>";
You have to change the line to :
echo '<img src="data:image/jpeg;base64,'
.base64_encode($image['file_data']).'" width=300 height=200/>";
In my experience, the problem my image was broken when I tried to display it from database is the the length of the image, I mean from the database where you put the length of a varchar you should change it to long text.
Your image source should be image file extension not php extension, Please check :
echo "<img src='any jpg,png or gif exetension path' width='300' height='200' />";
for example:
echo "<img src='imagename.png' width='300' height='200' />";
is it possible to pass through $_POST or $_GET an array, with values in it, without using serialize() and unserialize() ?
here is an example code, trying to find a number..i entered value 4 instead of rand, just to do the testing..
i thought of the potential of using a foreach to make multiple input hidden, in case i could pass all variables every single time, but it seems not to be working..
any ideas..??? or it is just not possible without serializing?
<?php
$x = $_POST['x'];
$Num = $_POST['Num'];
$first_name[] = $_POST['first_name'];
if (!$x)
{
Echo "Please Choose a Number 1-100 <p>";
$x = 4; //rand (1,4) ;
}
else {
if ($Num >$x)
{Echo "Your number, $Num, is too high. Please try again<p>";}
elseif ($Num == $x)
{Echo "Congratulations you have won!<p>";
Echo "To play again, please Choose a Number 1-100 <p>";
$x = 4;// rand (1,4) ;
}
else
{Echo "Your number, $Num, is too low. Please try again<p>";}
}
?>
<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post"> <p>
Your Guess:<input name="Num" />
<input type = "submit" name = "Guess"/> <p>
<input type = "hidden" name = "x" value=<?php echo $x ?>>
<?php
foreach($first_name as $val){
echo "<input type=\"hidden\" name=\"first_name[]\" value=$val />";
}
?>
</form>
</body>
</html>
<?php
foreach($first_name as $k => $val)
echo "<input type='hidden' name='first_name[$k]' value='$val' />";
should work.
here is the solution that i figured out...
$x = $_POST['x'];
$Num = $_POST['Num'];
$first_name = $_POST['first_name']; //creating array from the begining
$first_name[] = $Num; // add current num to next available slot in array
$counter = $_POST['counter'] +1;
if (!$x) // below this is the same..
....
....
<input type = "hidden" name = "x" value=<?php echo $x; ?>>
<input type = "hidden" name = "counter" value=<?php echo $counter; ?>> //add a counter to count loops
<?php
if ($counter>=2){ //counter in first load of page will be 1, so we need to read enter value from the next load of page
for ($i=0; $i<=($counter-2); $i++){ // counter-2 gives us the actual number of elements
echo "<input type=\"hidden\" name=\"first_name[]\" value=$first_name[$i] />";
}
}
?>
</form>