Cakephp query only displays last value of array - php

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>';
}
}

Related

Getting duplicate values from mysql_fetch_assoc in while

I'm stuck with while in php this is the part that makes problem and still returning duplicate rows. When I use select in phpmyadmin I get what I'm looking for. I think problem is with while. Can you look at it?
<td><select name='movie_type'>
<?php
$query = 'SELECT movietype_id, movietype_label FROM `movietype`';
$result = mysql_query($query, $db) or die (mysql_error($db));
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $value) {
echo '<option value="' . $row['movietype_id'] . '">';
echo $row['movietype_label'] . '</option>';
}
}
?>
</select></td>
You don't need to use foreach loop, just change your code to:
while ($row = mysql_fetch_assoc($result)){
echo '<option value="' . $row['movietype_id'] . '">';
echo $row['movietype_label'] . '</option>';
}

PHP functions with MySQL queries

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");

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

selectbox with multiple columns from mysql database

I have the following php code and it's working great for showing 1 column, but I need it to show the values of 10 columns.
<select size="1" name="domeinnaam">
<?php
include '../config.php';
$sql = "SELECT * FROM megabase";
$resultaat = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($resultaat))
{
echo '<option>' . $row['domeinnaam1'] . '</option>';
}
?>
</select>
I have tried to add a 2nd echo, but that corrupted the code. I also tried to
echo '<option>' . $row['domeinnaam1'] . $row['domeinnaam2'] . '</option>';
but that didnt work. because the result will then display as follows:
domain1
domain1
domain1domain2
and it should be
domain1
domain1
domain1
domain2
What will work?
Assuming you want each domain name to appear as an option in the select and the domain name fields in your db are domeinnaam1, domeinnaam2, domeinnaam3, etc., you would do the following...
<?php
include '../config.php';
$sql = "SELECT * FROM megabase";
$resultaat = mysql_query($sql) or die (mysql_error());
$domains = array();
while ($row = mysql_fetch_array($resultaat))
{
if (!empty($row['domeinnaam1'])) $domains[] = $row['domeinnaam1'];
if (!empty($row['domeinnaam2'])) $domains[] = $row['domeinnaam2'];
}
?>
<select size="1" name="domeinnaam">
<?php
foreach ($domains as $domain)
{
echo "<option>$domain</option>";
}
?>
</select>
You should use PDO instead of mysql_ functions or the ADODB library works well. mysql_ functions are deprecated as of PHP 5.5
refer to http://www.php.net/manual/en/pdo.construct.php for PDO reference
Your code should not have major PHP configurations or initializations like this. I does not look smart. Tidy it up.
<select size="1" name="domeinnaam">
<?php
include '../config.php';
$sql = "SELECT * FROM megabase";
$resultaat = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($resultaat))
{
echo '<option>' . $row['domeinnaam1'] . '</option>';
}
?>
</select>
<?php
include '../config.php';
$sql = "SELECT * FROM megabase";
$resultaat = mysqli_query($conn, $sql) or die ($conn->mysqli_error());
?>
<select size="1" name="domeinnaam">
<?php
while ($row = mysqli_fetch_array($conn, $resultaat))
{
echo '<option>' . $row['domeinnaam1'] . '</option>';
}
?>
</select>
Next don't really try to break options, you could use javascript frameworks like JQuery to achieve a more stylish select. I don't really think you can do this:
while ($row = mysqli_fetch_array($conn, $resultaat))
{
echo '<option>' . $row['domeinnaam1'] . '<br />' .$row['domeinnaam2']. '</option>';
}
Using JQuery, you could a list item in a div:
<ul>
while ($row = mysqli_fetch_array($conn, $resultaat))
{
echo '<li>' . $row['domeinnaam1'] . '<br />' .$row['domeinnaam2']. '</li>';
}
</ul>
Then pass the selected item to a hidden field for later use using the onclick event.
If you want to display the 10 columns in one loop as separate options, this could do it.
while ($row = mysqli_fetch_array($conn, $resultaat))
{
echo '<option>' . $row['domeinnaam1'] .'</option>';
echo '<option>' . $row['domeinnaam2'] .'</option>';
echo '<option>' . $row['domeinnaam3'] .'</option>';
echo '<option>' . $row['domeinnaam4'] .'</option>';
echo '<option>' . $row['domeinnaam5'] .'</option>';
echo '<option>' . $row['domeinnaam6'] .'</option>';
echo '<option>' . $row['domeinnaam7'] .'</option>';
echo '<option>' . $row['domeinnaam8'] .'</option>';
echo '<option>' . $row['domeinnaam9'] .'</option>';
echo '<option>' . $row['domeinnaam10'] .'</option>';
}
I wonder where this would be useful and how. Anything can happen in programming. :)

How to pass array value in javascript

I want to pass php value in javascript. I run a query and apply if statement and while array on javascript. But always show empty result. I can't find the problem. anyone please help me. Thanks
<?php
session_start();
$SUserName=$_SESSION['view'];
include 'dbconnect.php';
$query="select * from user_permission where username='$SUserName'";
$result=mysql_query($query) or die (mysql_error());
?>
<script type="text/javascript">
d = new dTree('d');
d.add(0,-1,'Dhuronto');
<?php
if($SUserName=='sumon#dhuronto.com')
{
echo "d.add(1,0,'Admin','blank.php', 'Admin', 'main');";
}
else {
while ($row = mysql_fetch_array($result))
{
echo "d.add('$id','$pid','$node','$url', '$node', 'main');";
}
}
?>
</script>
Where are '$id','$pid','$node','$url', '$node' coming from?
while ($row = mysql_fetch_array($result))
{
echo "d.add('$id','$pid','$node','$url', '$node', 'main');";
}
Did you mean
while ($row = mysql_fetch_array($result))
{
echo "d.add('$row[id]','$row[pid]','$row[node]','$row[url]', '$row[node]', 'main');";
}
NB - i wouldn't use the above syntax myself, it being the only time you can write vars as $row[id]. I prefer
echo "d.add('" . $row['id']. "','" . $row['pid']. "'...
You are using the names of the columns as if they are variables. You need to get them from the array:
while ($row = mysql_fetch_array($result))
{
echo "d.add('{$row['id']}','{$row['pid']}','{$row['node']}','{$row['url']}', '{$row['node']}', 'main');";
}
Your code does not work because you have not defined values for $id, $pid, and so on. That said, you can use json_encode() to JavaScript encode PHP data types properly, assuming your text is UTF-8 encoded:
while ($row = mysql_fetch_array($result))
{
// I don't know your database schema, so this could be wrong
$args = array(
$row['id'], 0, htmlspecialchars($row['username']),
'blank.php', '', 'main'
);
// echo 'd.add.apply(d,' . json_encode($args) . ');';
echo 'd.add(' . implode(',', array_map('json_encode', $args)) . ');';
}
Note that dTree is sort of an outdated JavaScript library that has fallen behind current best practices. It was last updated in 2003! You might want to look into newer, better alternatives such as those mentioned at https://stackoverflow.com/questions/1710114/jquery-tree-plugin.

Categories