I am trying to display only user selected pages in dashboard
I have two columns in database like this
create table users(userid varchar(50), scripts varchar(100))
in userid column i will have the logged in user name and in scripts column, names of the pages which they want to display it in dashboard in comma separated format. ex: total, cust_orders,...
I want to fetch page name from scripts column separately like total.php, cust_orders.php...
i tried doing like this
$sql = "select scripts from users where userid = '".$_SESSION['UserID']."' ";
$result = DB_query($sql,$db);
$myrow = DB_fetch_array($result);
foreach ($myrow as $res)
{
$array123[] = $res;
$var123 = $array123[0];
$var222 = $array123[1];
}
but it wont work as the pages can be from 1 to 8, can somebody please help me in this?
EDITED
I have done like this
$result = DB_query($sql,$db);
$myrow = DB_fetch_array($result);
$arr= $myrow['scripts'];
$arr1 = explode(',', $myrow['scripts']);
print_r ($arr1);
and it worked, it displays like this
Array ( [0] => total_dashboard [1] => customer_orders [2] => unpaid_invoice [3] => lat
but dynamically how can i separate it and i have to add .php to this ...
$sql = "select scripts from users where userid = '".$_SESSION['UserID']."' ";
$result = mysql_query($sql,$db);
while($myrow = mysql_fetch_array($result))
{
$arr=explode(',',$myrow["scripts"]);//this will strip the , separated values in an array
//now you can fetch the scripts from database
}
You can use PHP - explode like:
while ($myrow = mysql_fetch_object($result))
{
// $myrow->scripts = "text1.php,text2.php,text3.php";
$scripts = explode(',', $myrow->scripts);
}
So $scripts will then contain each PHP-Page/Skript as own position in the array.
$scripts[0] = "text1.php";
$scripts[1] = "text2.php";
$scripts[2] = "text3.php";
Related
I'm trying to create an instant search to pull the data from my database.
I want it to be able to search 2 fields from 2 different tables. So far, I have it working that if you choose one specific field of either table, it will work just fine, currently; it is pulling the data and displaying it, but I want the search to be specific.
For example, let's say I have 2 tables, one is for artist, and the other one is for their records; when you type the name of the artist, it will show all the records form that artist, but if you type the date of the release alongside of the name, it will give you that specific record and it won't show others. That's how I want it to work.
Here's what I've got so far:
include 'conn.php';
$query = $_POST['query'];
$like = "'%$query%'";
$sql = mysqli_query($db, "SELECT * FROM users JOIN tshift ON users.id=tshift.uid WHERE name LIKE {$like}
LIMIT 6");
$arr = array();
while ($r = mysqli_fetch_assoc($sql))
{
$results= array();
foreach($r as $key => $val){
if(is_string($key))
$results[$key] = $val;
}
array_push($arr, $results);
}
echo json_encode(array("artist" => $arr,"album" => $arr,"release" =>$arr));
for those that wonder or will wonder one day how to do something like this, this is how i got it to work i don't know if is the best way but it will do for now.
include 'conn.php';
$query = $_POST['query'];
$newQuery = explode("+", $query);
$name = "'%{$newQuery[0]}%'";
if(!empty($newQuery[1])){
$date = "'%{$newQuery[1]}%'";
}
if(empty($date))
{
$select = "SELECT * FROM users JOIN tshift ON users.id=tshift.uid WHERE name LIKE {$name}";
}
else
{
$select = "SELECT * FROM users JOIN tshift ON users.id=tshift.uid WHERE name LIKE {$name} AND udate LIKE {$date} ";
}
$sql = mysqli_query($db,$select);
$arr = array();
while ($r = mysqli_fetch_assoc($sql))
{
$results= array();
foreach($r as $key => $val){
if(is_string($key))
$results[$key] = $val;
}
array_push($arr, $results);
}
echo json_encode(array("artist" => $arr,"album" => $arr,"release" =>$arr));
$sql = "SELECT name FROM students";
$result = $conn->query($sql);
According to code above the $result is an object which contains the values from database.
suppose there are two names in the database under the column "name" like name1 and name2
now what i want is to convert the object $result into an array which will contain name1 and name2 as array element like
$name_array = array('name1' , 'name2')
how can i do that ??
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// acess the each colum elements using $row["<col name>"]
//this will loop through all the rows
}
} else {
// no rows where fetched
}
That is $row = $result->fetch(); will store the values of a row
in the array $row with key as column names
Answering my own question
got a solution for my problem and realized that it's pretty basic and shouldn't have posted this question. this type of problem should be solved by oneself.
$query = "SELECT name FROM students";
if($query_run = mysql_query($query)) {
$name_array = array();
while($query_row = mysql_fetch_assoc($query_run)) {
array_push($name_array, $query_row['name']);
}
the array_push function will do the trick.
thank you.
I'm trying to retrieve multiple rows from joining two tables where store.itemid = item_list.id.
$query = "SELECT s.price, il.*
FROM store s LEFT JOIN item_list il ON s.itemid = il.id";
I then have:
if($result = $conn->($query)) {
$array = $result->fetch_array(MYSQLI_ASSOC);
}
With my current code, the query is only retrieving the first row from the 'store' table. I have made certain that there should definitely be more than one row to return.
print_r($array) shows:
Array ( [price] => 400 [id] => 5 [name] => Computer )
That's because you are only running fetch_array() once. You probably need to run it in a loop, e.g.:
if ($result = $conn->query($query)) {
while ($array = $result->fetch_array(MYSQLI_ASSOC)) {
// do something with $array
}
}
instead of this:
$array = $result->fetch_array(MYSQLI_ASSOC);
use this:
while($row = $result->fetch_array(MYSQLI_ASSOC)){
//put your code here!
}
I want to select some data from db and store in an array. Suppose I have a column "keyword" in my db table. I want to select all rows where keyword column is like "nature".
I am trying following code:
<?
$term= "nature";
$arr = array();
$sql = "select keyword from keywords where keyword LIKE '%$term%'";
$result = mysql_query($sql) or die(mysql_error());
$rows = mysql_fetch_array($result);
foreach ($rows as $row){
array_push($arr, $row['keyword']);
}
print_r($arr); //output: Array ( [0] => n [1] => n )
?>
So the result from db should return only one keyword 'nature' which i need to store in array.
Why it is storing same string two times? There is no any other row in db similar to the term nature.
Why it is storing only first letter in the array? Shouln't it store "nature" instead of "n"?
Please help me fixing this.
Should be something like
$term = "nature";
$arr = array();
$sql = "select keyword from keywords where keyword LIKE '%$term%'";
$result = mysql_query($sql) or die(mysql_error());
while( $row = mysql_fetch_assoc( $result ) ) {
arr[] = $row[ 'keyword' ];
}
In your solution you only fetch the first record from the result-set as numeric indexed array.
Btw - you are aware that a LIKE-query starting with a wildcard cannot make use of any index?
use mysql_fetch_assoc instead of mysql_fetch_array
I have a list of users in my table. How would I go about taking that list and returning it as one PHP variable with each user name separated by a comma?
You could generate a comma-separated list with a query:
SELECT GROUP_CONCAT(username) FROM MyTable
Or else you could fetch rows and join them in PHP:
$sql = "SELECT username FROM MyTable";
$stmt = $pdo->query($sql);
$users = array();
while ($username = $stmt->fetchColumn()) {
$users[] = $username;
}
$userlist = join(",", $users);
You would fetch the list from the database, store it in an array, then implode it.
The best way I was able to figure this out was by storing the results from each col, or field, and then echoing the implode:
$result = mysqli_query($conn, $sql) //store the result
$cols = array(); //instantiate an array
while($col = mysqli_fetch_array($result)) {
$cols[] = $col['username']; //step through results and save each username in array
}
echo implode(",",$cols); //turn the array into a comma separated string and echo it
This took me a while to figure out exactly how to do this. Hope it helps!