MYSQL How can I make dropdown list? - mysql

I created a new table in workbench where I want to put a season of a soccer championship.
I don't want to write down every time the team's names, can you help me pls, how can I make like a dropdown list (or set of values, what are the only options for that column), when I begin to type "Bay..." its automatically offer "Bayern München" as the only option.

I don't know how much will this help, but I have made a dropdown list for something else and I hope my way might give you an idea:
foreach ($dbo->query($sql) as $row) //Array or records stored in $row
{
if($row[id]==10)
{
echo "<option value=$row[id] selected>$row[name]</option>";
}
else
{
echo "<option value=$row[id]>$row[name]</option>";
}
}
echo "</select>"; // Closing of list box

As the same answer above, but using short-hand ternary operator instead of if-else statement, and don't forget to use ' or " when reference to object string index.
foreach ($dbo->query($sql) as $row) //Array or records stored in $row
{
printf("<option value='%s' %s>%s</option>", $row['id'], $row['id'] == 10 ? 'selected' : '', $row['name']);
}
}
echo "</select>"; // Closing of list box

Related

How to insert data from a database to array and check it

I need to modify views in an application and I have table user privileges, with a structure like this below
id_staff | feature | capabilities
1 reports view
1 reports create
1 reports edit
2 reports view
2 reports delete
3 reports view
What I need, is where user like id_staff 3 that in reports, feature only have one capabilities, like the example he just can view, can not edit, edit, or create. Then user id 3 only sees hyperlink to view the page, he cannot see hyperlink to create, edit or delete pages.
To do that, I tried to use select query, insert that to array and then compare the array value to getting the condition like above
I have tried to code like this
<?php
$query = $this->db->query('SELECT * FROM tblstaff_permissions WHERE staff_id='.$id.'');
foreach ($query->result() as $row)
{
if($row['feature']=='reports' and $row['capabilities']=='view'){
View
} elseif($row['feature']=='reports' and $row['capabilities']=='delete' ){
Delete
}
}
?>
And then the page is blank
Do you know where's the error ?
Thank you
You could improve it by reducing the repetitive code, like the $row['feature']=='reports' code, like :
$query = $this->db->query('SELECT * FROM tblstaff_permissions WHERE staff_id='.$id.'');
foreach ($query->result_array() as $row)
{
if ($row['feature']=='reports') {
if ($row['capabilities']=='view') {
echo 'View';
} elseif ($row['capabilities']=='delete') {
echo 'Delete';
}
}
}
To get an array result, use the $query->result_array() instead of $query->result(), and also you misses the echo statement.
You are using $query->result() it will give you an array of objects, not an array of array. Also, you are not echoing your links on the view. One more thing you should not use the same condition repeatedly.
In this case, your code should be -
<?php
$query = $this->db->query('SELECT * FROM tblstaff_permissions WHERE staff_id='.$id.'');
foreach ($query->result() as $row) {
if($row->feature =='reports'){
if($row->capabilities=='view'){
echo 'View';
} elseif($row->capabilities == 'delete' ){
echo 'Delete';
}
}
}
?>

$index as a parameter : multiple if statement issue

Please take a look at this function :
function my_func($index_bis) {
if (!is_int($index_bis) || $index_bis > 5 || $index_bis < 0) return;
elseif ($index_bis = 5) : echo 'This';
elseif ($index_bis = 1) : echo 'That';
endif;
}
What's wrong with this? I actually can't understand where is the mistake...
Thank you for your help.
What is this? PHP?
And what are you attempting to do?
How about this:
function my_func($index_bis) {
if (!is_int($index_bis) || $index_bis > 5 || $index_bis < 0){
$str = $index_bis;
}
elseif ($index_bis == 5) {
$str='This';
}
elseif ($index_bis == 1) {
$str='That';
}
return $str;
}
It's pretty atypical to put anything after "return"; and I think "return" stops the function, effectively.
Edit: FOREACH method.
If you are building html from two related variables, FOREACH is a great method for handling them, as you can set a data "key" and "value" for each item in the array.
By default, the "key" in PHP is assigned a number, starting from 0 and progressing through increasing integers. You have the option of naming these keys, instead of using the PHP-assigned default.
So consider setting up your array as such:
$newArray = (
'tab name' => 'label name',
'apples' => 'red',
'oranges' => 'orange',
);
Wherein the item to the left of the "=>" is always the key, and the item to the right is always the value.
Then, you can add more elements to this array in your code by using this line:
$newArray['bananas'] = 'yellow';
Note the key (tab) name, with square brackets (because it is the key name) and single quotes (because it is a string).
Now that you have an array, you can "walk" it using a FOREACH loop and echoing the html. Here is the code:
foreach($newArray as $key => $value) {
echo "<tab>$key</tab> : ";
echo "<label>$value</label>
echo "<br /";
}
This will output the html below:
<tab>tab name</tab> : <label>label name</label><br />
<tab>apples</tab> : <label>red</label><br />
<tab>oranges</tab> : <label>orange</label><br />
<tab>bananas</tab> : <label>yellow</label><br />
Very useful. Have fun!
Note: There are likely better ways to do this, but I'm sharing this one as a simple solution. And also, because you shouldn't go further without understanding both FOREACH and simple arrays.
Note: If you want to incorporate more than two variables, you will need a more complex data structure. In PHP, these are called "multidimensional arrays". Give it a read. Beyond that, there are still more methods for handling data (ie. "objects").

echo a variable from a multidimentional array outside a function

The code below works for a string value but not when I try to access the variable directly.
The data being accessed is a table at http://webrates.truefx.com/rates/connect.html?f=html
My code strips it of tags and put it in an array $row0
And puts it in a function. But I can't get it out. The function is simplified for this question. I intend to concatenate some of the variables inside the function once I find out what I'm doing wrong.
$row0 = array();
include "scrape/simple_html_dom.php";
$url = "http://webrates.truefx.com/rates/connect.html?f=html";
$html = new simple_html_dom();
$html->load_file($url);
foreach ($html->find('tr') as $i => $row) {
foreach ($row->find('td') as $j => $col) {
$row0[$i][$j]= strip_tags($col);
}
}
myArray($row0); //table stripped of tags
function myArray($arr) {
$a = 'hello'; //$arr[0][0]; HELLO will come out but not the variable
$b = $arr[1][0];
$r[0] = $a;
$r[1] = $b;
//echo $r[1]; If the //'s are removed one can see the proper value here but not outside the function.
return $r;
}
$arrayToEcho = myArray($arr);
echo $arrayToEcho[0]; // will echo "first"
I have tried all the suggestions from here:
http://stackoverflow.com/questions/3451906/multiple-returns-from-function
http://stackoverflow.com/questions/5692568/php-function-return-array
Suggestion appreciated please and more info available if required. Thank you very much for viewing.
You need to get the innertext of $col in your loop. Like this:
$row0[$i][$j]= $col->innertext;
The next thing is:
myArray($row0);
This call will correctly return the parsed array; try echoing it and you'll see. But when you do this:
$arrayToEcho = myArray($arr);
...you're referencing to $arr which is a local variable (a parameter, actually) inside your function myArr. So what you probably meant was this:
$arrayToEcho = myArray($row0);
Hope this helps!
UPDATE
Look, I show you what happens when you call a function:

How can I choose only 2 columns without changing the SELECT query?

I am generating a table with this code:
if ($arch = $pdo->prepare("SELECT one, two, three, four FROM numbers)) {
$arch ->execute();
$data = $arch->fetchAll();
echo '<table>';
foreach ($data as $row){
echo '<tr>';
foreach ($row as $col){
$col=nl2br($col);
echo '<td>'.$col.'</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
This populates a table with 4 columns.
How can I choose only 2 columns without changing the SELECT query?
In other words, how can I choose the columns I want to populate the table? for example I would like to use only "three" and "four"...
I read about fetchAll(PDO::FETCH_COLUMN, 0) but this seems to pick only one column. The same goes for fetchColumn(). I want to pick more than one so I can populate my table with only the columns I need.
Just add a selector on the second foreach, to filter the columns you want:
....
foreach($row AS $col => $value){
if($col == 'your_column_name'){
echo '<td>'.nl2br($value).'</td>';
}
}
....
Make sure you have your 'fetchmode' on associative, which makes sure the pdo-result object contains a associative array with key-indexes.
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
Alternatively, you can specify the columns you want as an array beforehand, and iterate over that array rather than the columns in the row.
$columns = array('I_like_this_column', 'this_one_too');
foreach ($columns as $colName)
echo '<td>' . nl2br($row[$colName]) . '</td>';
This has the added advantage of letting you specify the order the columns will appear in your output without changing your query.
And, as with #stUrb's answer, you need to turn on PDO::FETCH_ASSOC for this to work.

html listbox to show additonal txt in extra field outside of listbox

I have the following db-retrieve which results in the Listbox I like to have.
But I need $desc2 to be shown under the listbox in a separate field and it must change its content when the user clicks on an other item in the list:
Here is the retrieve which works:
echo "<form action=\"admin.php\" method=\"post\">";
echo "Industry:<SELECT name=\"industry\" size=\"10\">";
$result=sql_query("SELECT cat, title, desc, parentid
FROM industries
WHERE language='english'");
while(list($cid2, $ctitle2, $desc2, $parentid2) = sql_fetch_row($result)) {
if ($cid2==$userindustry) {
$sel = "selected";
} else {
$sel = "";
}
if ($parentid2!=0) $ctitle2=getparentindustry($parentid2,$ctitle2);
echo "<option value=\"$cid2\" $sel>$ctitle2</option>";
}
echo "</SELECT>
<br>$desc2<br> # place to show description of list item
<input type=\"hidden\" name=\"op\" value=\"save\">
<input type=\"submit\" value=\"Go\"></form><br>";
As I'm now searchin for some time, but didn't found something, hopefully someone here could help me.
The code for the side is in php.
Thanks in advance.
You would have to store $desc2 to a temporary variable in the loop, and use the temporary variable after the select to show the temporary variable.
I would however, point out that in general, this is probably the wrong way of going about this code, and that your problem is deeper in your implementation :)