How to make a php variable $_POST upon clicking it? - php

I'm trying to make $suggestion[$ss_count] become a clickable link that does a $_POST once it is clicked. I'm trying to achieve a query expansion sort of an effect. The word itself of course needs to be the information posted and it needs to be recognized as
if ($_POST['query'])
The code I use for this is:
<?php
if ($_POST['query'])
{
$query = urlencode($_POST['query']);
$s_count = 0;
$ss_count = 0;
$query = 'http://www.dictionaryapi.com/api/v1/references/collegiate/xml/'.$query.'? key=135a6187-af83-4e85-85c1-1a28db11d5da';
$xml = new SimpleXMLIterator(file_get_contents($query));
foreach ($xml -> suggestion as $suggestion[$s_count])
{
$s_count++;
}
if ($s_count > 1)
{
echo ('<h4>Did you mean?</h4>');
while ($ss_count <=$s_count)
{
echo ($suggestion[$ss_count].'<br>');
$ss_count++;
}
}
}
?>

Related

How to fix Array overwritten after foreach loop php

I am having issue data Array overwritten in foreach loop. Result I am getting like this wrongRight together .Right answer is showing but also wrong for example ZucchiniCauliflower.Please help
CODE 1
$data = array();
$dis_07= null;
$dis_03 = null;
if (is_array($row)) {
foreach ($row as $value) {
$gccat_id = $value->gccat_id;
$ccat_id = $value->ccat_id;
$cat = $value->cat_id;
if (isset($gccat_id) && $gccat_id == $id) {
$dis_07 = $value->category;
$dis_02 = $value->child_id;
}
if (isset($ccat_id) && $ccat_id == $id) {
$dis_03 = $value->category;
$dis_02 = $value->parent_id;
}
}
}
$data['Dis_03'] = $dis_03;
$data['Dis_07'] = $dis_07;
if (isset($data['Dis_03'])) {
echo $data['Dis_03'];
}
if (isset($data['Dis_07'])) {
echo $data['Dis_07'];
}
First I tried this way But In one I was getting right in second link I am getting right So Tried the code previous one .In the prvious I am getting correct and wrong one together EExample ZucchiniCauliflower
CODE 2
if (isset($id)) {
$db = Database::newInstance();
$data = array();
$data['cat_status'] = 1;
$sql = "SELECT * FROM category WHERE cat_status=:cat_status ";
$row = $db->read($sql,$data);
$data['id'] = $crypt->decryptId($id);
echo $data['id'];
$id=$data['id'];
if (is_array($row)) {
foreach ($row as $value) {
$gccat_id=$value->gccat_id;
$ccat_id = $value->ccat_id;
$cat = $value->cat_id;
if (isset($gccat_id) && $gccat_id == $id) {
$data['Dis_03']=$value->category;
}
if (isset($ccat_id) && $ccat_id == $id) {
$data['Dis_03'] = $value->category;
break;
}
}
}
}
--------------------------READ FROM HERE------------------------
Here is a link one when I click on this link
$id=$value11->gccat_id;
$title
I expected the output is
Home>Raspberry
Here is a link Second link when I click on this link
Here id is ($value11->gccat_id)
window.open('<?= BASEURL ?>ap/'+id,'_self');
I expected the output is
Home>Cauliflower
1. WHEN I Use the Code 2 (Added break in this condition
(isset($ccat_id) && $ccat_id == $id)) Then click on link second
it gives output Home>Cauliflower which I was expecting. It is
correct.
2. But this time as I added the break in (isset($ccat_id) && $ccat_id == $id). I click on link one It gives wrong output which I was not expecting. Home>Squash which is wrong.
In one link I was expecting
Home>Cauliflower
ERROR NOTE If I add Break; then link Second gives correct output but when I remove Break; then link one give correct. I wanted Both link should give correct output.
Problem was with cat_id,ccat_id ,gccat_id.
I provided 8 digit unique number with the following output,now I am getting the correct output.
function generateUniqueNumber() {
return sprintf('%08d', mt_rand(1, 99999999));
}

How to get a single variable attached to the URI?

I don't seem to able to acquire a GET variable that is attached to the URI.
The codes are => at the controller
...
parse_str($_SERVER['QUERY_STRING'], $_GET);
....
$data['ti'] = $this->input->get('hamleno');
this->load->view('anasayfa', $data);
The codes are => at the view the link is
...
<div class="row"><?php for ($b=0; $b<=(count($hml)-1); $b++) { ?><?php echo $hml[$b]." "; ?> <?php } ?></div>
The link is working. I have added the
$config['uri_protocol'] = "PATH_INFO";
to the config.php file.
However I am not able to get the $ti variable
if ($ti){
$t=$ti;
}else{
$t = $this->input->post('t');
if (!$t) $t = 0;
if( $this->input->post('ileri') ) {
$t=$t+1;
if($t>($uz-1)){
$t=$uz-1;
}
} // Forward button was pressed;
if( $this->input->post('geri') ) {
$t=$t-1;
if($t<0){
$t=0;
}
} // Back button was pressed;
}
I'm not familiar with codeigniter, but I've always passed GET variables in this manner:
URL = www.site.com/folder/webpage.php?myvariable=myvalue
I would retrieve that value this way:
$x = $_GET['myvariable'];
or with codeigniter: (I think)
$x = $this->input->get('myvariable');
Tailored to your example, I would personally de-obfuscate your loop code just a little and instead of switching from PHP to HTML and back in one line, I would simply echo both from PHP like this:
(I also don't exactly understand the url you're using, so here is my approximate)
<?php
for ($b=0; $b<=(count($hml)-1); $b++)
{
echo '',$hml[$b],' ';
}
?>
I found out how Codeigniter solves the problem =>
$get = $this->uri->uri_to_assoc();
if(isset($get['hamleno'])){
$data['ti'] = $get['hamleno'];
}
This sends the $b assigned to $hamleno together with all the other stuff in $data.
Thank you all for your kind comments

Passing array of data to other page using session in PHP

I have some problem passing array of data in Php using session:
In my class, I have this function:
public function check_dupID($id)
{
$this->stmt="select id from student where id='$id'";
$this->res = mysql_query($this->stmt);
$this->num_rows = mysql_num_rows($this->res);
if($this->num_rows == 1)
{
while($this->row = mysql_fetch_array($this->res))
{
$dup_id = $this->row[0];
return $dup_id;
}
}
}
This code checks for duplicate ID of a student. Now, the reason for this is that I uploaded the file in the database using ".csv" file.
Before Uploading it I put all the data in the csv file into an array. Now, I check it one by one:
//created the variable for checking
$id = $check->check_dupID($array[0]);
if($id == TRUE)
{
session_start();
$_SESSION['id']; = $id;//passing it to the next back to array again.
$redirect->page($error);
}
Now when it redirect it gives me the result of an "Array" and if I parse it using foreach it shows only one id but there is more than of that. I want to know how to display it all.
This code will return the duplicate id when it is found for the first time. If you want to return all the duplicate ids, then add them into an array using the while loop and then return it.
To do this modify your code like this:
public function check_dupID($id)
{
$this->stmt="select id from student where id='$id'";
$this->res = mysql_query($this->stmt);
$this->num_rows = mysql_num_rows($this->res);
$dup_id = array();// added here to prevent invalid argument supplied to foreach loop error if nothing is found
if($this->num_rows >1)
{
while($this->row = mysql_fetch_array($this->res))
{
$dup_id[] = $this->row[0];// adding duplicates into the array
}
return $dup_id;
}
}
This code fixes my problem:
if($id == TRUE)
{
$get_id = array();
$get_id[] = $id;
session_start();
$_SESSION['id']; = $get_id;//passing it to the next back to array again.
$redirect->page($error);
}

How to convert Meters to Foot using PHP

I have used this code which shows an error:ie if my meters is 175m but when i convert it does'nt show that thing
<?php
function metersToFeetInches($meters, $echo = true)
{
$m = $meters;
$valInFeet = $m*3.2808399;
$valFeet = (int)$valInFeet;
$valInches = round(($valInFeet-$valFeet)*12);
$data = $valFeet."′".$valInches."″";
if($echo == true)
{
echo $data;
} else {
return $data;
}
}
?>
<?php
$feetInches = metersToFeetInches(1.75,false);
echo $feetInches;
?>
This is one of those things you can easily find on Google, but I guess you didn't look further than the first link.
Anyways, you should do it like this.
<?php
function metersToFeet($meters) {
return floatval($meters) * 3.2808399;
}
?>
But why is this better than the code you posted? Well, functions are not supposed to do everything. You should write a function for a certain action, not one big function with everything you can every need. Because if you need to add something to that function or change the order of actions, it's a hell of a lot of work.
Furthermore, your function has an option $echo. But why would you need that? You can add such an option to every function, but PHP has a nice commando for that: echo. So instead, it's way better to write echo metersToFeet(10). Or $value = metersToFeet(10) if you need to save the result in a variable.
<?php
function metersToFeetInches($meters, $echo = true)
{
$m = $meters;
$valInFeet = $m*3.2808399;
$valFeet = (int)$valInFeet;
$valInches = round(($valInFeet-$valFeet)*12);
$data = $valFeet."′".$valInches."″";
if($echo == true)
{
echo $data;
} else {
return $data;
}
}
?>

How to implement this image preloader into my php file?

I want to have my images which are processed via ajax to be preloaded visually rather then the usual loading of images where you see them build up from top to bottom. Please take a look here. What you see there is what I want to have in my php file visually.
The part where the pictures are loaded from the database are the following:
$sql = "SELECT * FROM `fabric`".$filter;
$result=mysql_query("$sql");
//echo $sql;
$data = "";
$ii = 0;
$m = 0;
while($myrow = mysql_fetch_array($result)){
$ii++;
$m++;
if ($m == 1) $data = $data."<div class=\"page current\">";
elseif ($ii == 1) $data = $data."<div class=\"page\">";
$data = $data."<img src=\"".$image_directory.$myrow['thumbnail']."\" width=\"100 px\" height=\"100 px\"><div class=\"fb_name\">".$myrow['name']."</div>\n";
if ($ii == 10) {
$data = $data."</div>";
$ii = 0;
}
}
if ($ii != 10) {
$data = $data."</div>";
}
if (empty($data)) echo "No result";
else echo $data;
?>
I really don't know how to change the php in order to have the effect shown in the demo.
Thank you for all constructive advices and forgive my noobiness :)
You will need javascript for this, you can´t do it in php alone. The basic idea is to hide your images using for example a visibility: hidden and have them fade in when they are completely loaded. And at load-time you position a loading image on top that you remove when you start fading in the image.
As the example you've given, is a plugin, perhaps you can even use that one instead of writing it yourself.

Categories