PHP functions with MySQL queries - php

I'm making simple basketball stats plugin to wordpress, and I'm using dropdown list a lot. I wanted to make function but I don't know how to pass arguments to MySQL. Here's my code:
function dropDown($tab, $option, $text){
$result = mysqli_query($con,'SELECT * FROM tab');
while($row = mysqli_fetch_array($result)){
echo "<option value=\"";
echo $row['option'] . "\">" . $row['text'];
echo "</option><br>";
}
}
and I would like to use it like this:
dropDown("team", "team_id", "name");
I tried with different quotation marks, dots etc but it doesn't seem to work.
#edit
I know PHP syntax (some of it) and I know how to use it, but I don't know how to pass $variables to MySQL query, and that's my main problem.

try
function dropDown($team, $team_id, $name) {
// use both three var where you want
$result = mysqli_query($con,'SELECT * FROM team');
echo "<select>";
while($row = mysqli_fetch($result)){
echo "<option value=\"";
echo $row['team_id'] . "\">" . $row['name'];
echo "</option>";
}
echo "</select>";
}
dropDown("team", "team_id", "name");

Related

Placing an option value into a table

came across a problem for my coursework when trying to place selected items from my database into a table, here is my code:
echo '<br>';
$describeQuery='Select Distinct current_location From Current_Location';
$results = sqlsrv_query($conn, $describeQuery);
echo '<br>';
echo '<br>';
echo "<table border='1', width='15%'><tr><th>Locations</th></tr>";"
while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
echo "<tr><td>" "<option value=\"Location1\">" . $row['current_location'] . </option>" "</td></tr>";
}
echo "</table>";
Can anyone see where I am going wrong? The error mentions that I have no ',' or ';' tags, therefore it never ends, however I cannot find this error through messing with the program a bit.
Sorry for any formatting issues, still adjusting to the website.
There is an extra double-quotes at the end of the 6th line and then you should modify the while loop's echo like this:
echo '<br>';
$describeQuery='Select Distinct current_location From Current_Location';
$results = sqlsrv_query($conn, $describeQuery);
echo '<br>';
echo '<br>';
echo "<table border='1', width='15%'><tr><th>Locations</th></tr>";
while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
echo "<tr><td><option value='Location1'>" . $row['current_location'] . "</option></td></tr>";
}
echo "</table>";

Passing a value through URL

Here is what i want to achieve ; sending ID's through URL's and printing it.
index.html
ID 1
ID 2
receive.php
<?php
$id_q = $_GET['id'];
print "The parameters passed through URL are $id_q";
?>
This above code works perfectly, I'm not able to do this with a list of ID's printed with a php command.
The below code is used to print all the PID's in the DB.How do i make every PID printed clickable ?
When I add html tags inside PHP code it throws up an error.
print.php
$result = mysqli_query($con,"SELECT * FROM List");
while($row = mysqli_fetch_array($result))
{
echo $row['PID'];
}
edit-query.php
$pid_q=$_GET[pid];
echo $pid_q;
while($row = mysqli_fetch_array($result))
{
echo "<a href='receive.php?id=".$row['PID']."'>".$row['PID']."</a>";
}
If you want to add your own text to a variable or echo, quote it and separate the variable with a "."
echo ''.$row['PID'].'';
you should do that like this
How about...
echo '' . $row['PID'] . '';
I believe this is what you mean?
while($row = mysqli_fetch_array($result))
{
echo 'Print ID: ' . $row['PID'] . '';
}
while($row = mysqli_fetch_array($result))
{
echo "<p id=".$row['PID']." class='clickable'>" . $row['PID'] . "</p>";
}
$(document).ready(function(){
$("#clickable").click(function(){
$(this)...something...
});
});
This is a little something you can do using JQuery if you wanted each PID to do something other than refer to another location. It will listen on any with the clickable class.

How to loop the $row = mysql_fetch_array($rs);

I have a table
Now.i have a function in my JS
function add()
{
<?php
include('conn.php');
$rs = mysql_query("select * from position");
$row = mysql_fetch_array($rs);
$ss=$row['Name'];
$sss=$row['nowb'];
$ssss=$row['totalb'];
$sssss=$row['nowc'];
$ssssss=$row['totalc'];
echo "add2()";
?>}
function add2(){
AddAddress("<?php echo $ss;?>","<?php echo $sss;?>/<?php echo $ssss;?><br /><?php echo $sssss;?>/<?php echo $ssssss;?>");
}
How to get the every date from my table?
Something like this?
function add() {
<?php
include('conn.php');
$rs = mysql_query("select * from position");
while ( $row = mysql_fetch_array($rs) ) {
$ss=$row['Name'];
$sss=$row['nowb'];
$ssss=$row['totalb'];
$sssss=$row['nowc'];
$ssssss=$row['totalc'];
echo 'AddAddress("' . $ss . '","' . $sss . '/' . $ssss . '<br />' . $sssss . '/' . $ssssss . '");';
}
?>
}
Didn't text the echo 'AddAddress....' line so I hop eI got all the single and double quotes in the right place??
Performing POST requests using Ajax here is an example of sending data from js to php.
also stop naming your vars s,ss,sss,ssss you will have no idea what they mean when you read your code tomorrow..
and try not to use mysql_* functions they have been deprecated switch to mysqli or pdo
I got what would you like to do. In your PHP file:
function add(){
<?php
include('conn.php');
$rs = mysql_query("select * from position");
echo "var data = [] ; "
while($row = mysql_fetch_assoc($rs)){
echo "
data.push({
name: '{$row['Name']}',
nowb: '{$row['nowb']}',
totalb: '{$row['totalb']}',
nowc: '{$row['nowc']}',
totalc: '{$row['totalc']}'
}); \n\r " ;
}
?>
add2(data);
}
function add2(data){
for (var i in data){
var row = data[i] ;
AddAddress(row.name, row.nowb, row.totalb, row.nowc, row.totalc);
}
}
If I understand the question correctly you want to know how to loop through an array in php.
$row = mysql_fetch_array($rs);
foreach($row as $value){
//Do something
}
Read up on it in the docs
http://php.net/manual/en/control-structures.foreach.php

Cakephp query only displays last value of array

I'm sure I'm doing something wrong here but I can't figure it out. I have tried several ways but no luck.
This is my controller:
$result = mysql_query("SELECT name FROM spa_packages") or die(mysql_error());
$names=array();
while ($row = mysql_fetch_row($result)) $names[]=$row[0];
mysql_free_result($result);
foreach ($names as $asd => $lol) {}
$this->set('anything', $lol);
This is where I display it:
if(!empty($anything)) {
echo " '<option value=" . $anything . '">' . $anything . '</option>';
}
Further to my comment and in more of an answer. You are doing something wrong. There is absolutely no reason to use mysql_ functions at all (they are deprecated) and you shouldn't be using any direct db access in an MVC framework like CakePHP
Your code should be more along the lines of
In your Controllers/SpaPackagesController.php index action:
$spaPacakges = $this->SpaPackage->find('all');
$this->set('spaPackages', $spaPackages):
In your Views/SpaPackages/index.ctp
<?php foreach ($spaPackages as $package): ?>
<!-- some html -->
<?php endforeach;?>
You really need to run through some CakePHP beginner tutorials as your question shows a big misunderstanding in how the framework works
Looks like you are trying to do this:
$result = mysql_query("SELECT name FROM spa_packages") or die(mysql_error());
$names = array();
while ($row = mysql_fetch_assoc($result)) {
$names[] = $row["name"]
}
$this->set('anything', $names);
mysql_free_result($result);
And:
if(!empty($anything)) {
foreach($anything as $name){
echo " '<option value=" . $name . '">' . $name . '</option>';
}
}

dynamic populate a select input from mysql

Hi I am trying to populate an entire Drop down list with MySQL but I cant get it to work, can you please help?
My code:
$database=& JFactory::getDBO();
$database->setQuery('SELECT training_id,training,trainingDate FROM training ');
$result = $database->loadObjectList();
echo '<select name="whatever">';
while($row = mysql_fetch_array($result)) {
echo '<option value="$row[training_id" />';
}
echo '</select>';
Your echo string doesn't allow for embedded variables because you are using single quotes instead of double quotes.
Implement this echo instead:
echo '<option value="' . $row["training_id"] . '" />';
Without knowing what the output is, it's hard to know whether this is the only issue, but the glaring error in your code is this:
echo '<option value="$row[training_id" />';
Because this is in single quotes, the variable is not interpreted. You need to use double quotes (and close the square brackets!):
echo "<option value=\"{$row['training_id']}\" />";
Note that I have changed the style of variable interpretation to use curly brackets: I believe this is easier to read and clearer.
In addition to using single quotes, you are also:
Missing a closing square bracket.
Missing the closing tag for the <option>.
You probably want to change your output to something like this, so you display some option text to the user:
echo '<select name="whatever">';
while($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['training_id'] . '"> ' . $row['training'] . '</option>';
}
echo '</select>';
$database= &JFactory::getDBO();
$database->setQuery('SELECT training_id,training,trainingDate FROM training');
$result = $database->loadObjectList();
echo '<select name="whatever">';
foreach ($result as $row) {
echo '<option value="'.$row->training_id.'" />';
}
echo '</select>';
Use #__ for table prefix.
$database= &JFactory::getDBO();
$database->setQuery('SELECT training_id,training,trainingDate FROM training');
$result = $database->loadObjectList();
echo '<select name="whatever">';
foreach ($result as $row) {
echo '<option value="'.$row->training_id.'" />';
}
echo '</select>';
This code works, but populates the list with empty options. I had to put a simple echo in as shown below, and works just fine. For some reason, $row->teremnev is empty for me. Im sure this is not the right way, but it works.
$db =& JFactory::getDBO();
$query = "SELECT teremnev FROM #__teremlista";
$db->setQuery($query);
$result = $db->loadResultArray();
echo '<select name="termek">' ;
foreach ($result as $row) {
echo '<option value="'.$row->teremnev.'" />';
echo $row;
}
echo '</option>';
echo '</select>';
<select>
<option>Select Training</option>
while($row = mysqli_fetch_array($result))
{
echo "<option>". $row['training_name']."</option>";
}
</select>

Categories