logo
down
shadow

Getting Database in array and using it outside with foreach statement like and Opencart


Getting Database in array and using it outside with foreach statement like and Opencart

By : ravi4974
Date : November 21 2020, 04:03 PM
this will help As Aman Chhabra mentioned, you must first fetch the result from database and then you can use foreach loop to iterate over it. One thing I would like to mention is DON'T use mysql_query() as use of it is discouraged as per the new guidelines of php development (Check: http://ca1.php.net/manual/en/function.mysql-query.php). To have a compatible code with advanced php use mysqli_query() instead. Following is the code example utilizing mysqli class. This is procedural way but you can use it in OOP style as well.
code :
//Connect to database
$host = "localhost"; //Change according to yours
$username = "root"; //Change according to yours
$password = ""; //Change according to yours
$database = "test"; //Change according to yours

$con = mysqli_connect($host,$username,$password,$database); //Create the connection
if(!$con)
{
    echo "Not connected";
}
else
{
    echo "Connected<br />";
}

//Prepare the query to fetch the database records
$query = "select * from TableName"; //Replace the table name with yours
$sql = mysqli_query($con,$query); //Execute the query
while($result = mysqli_fetch_assoc($sql)) //Loop through, till there are records corresponding to the query
{
    $rows[] = $result; //Store all the records in an array
}

//Now iterate over each property using foreach loop
foreach($rows as $row)
  {
      echo "Name - ".$row['name']." Age - ".$row['age']."<br />";
  }


Share : facebook icon twitter icon
foreach loop with in foreach to insert unique record in database for each item in array

foreach loop with in foreach to insert unique record in database for each item in array


By : wikiwert
Date : March 29 2020, 07:55 AM
wish of those help I have two arrays one is associative and second one is non associative i want to insert for each item of each array as a row in the database. In my code below $menu and $id are array . This $id part is giving me error as this itself is an array .... error says "array to string conversion". Please help!!! , If I understood what you are trying to do, this would be a way:
code :
$i = 0;
$id = $request->themeLocation;

foreach($menu as $label => $link) {
    DB::table('menus')->insertGetId([ 'label' => $label ,'url' => $link ,'child_id' =>'child-'.$id[$i] ,'menu_status' => 1  ]);
    $i++;
}
If/else statement with foreach item in database possible?

If/else statement with foreach item in database possible?


By : Max2phresh
Date : March 29 2020, 07:55 AM
will help you A link will be index.php?page=PAGENAME. The PAGENAME is mostly stored in the database, but some are static. I want an if/else statement to echo the title. The static part is not so hard. , Try this:
code :
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$sql = "SELECT * FROM countries";
$result = $conn->query($sql);

while($row = mysqli_fetch_assoc($result)) {
  $iso = $row['countryISO'];
  if ($current == $iso) {
    echo $row['countryname'];
  }
}
$conn->close();
How to implode data in array with commas from database column where has value = 1 in foreach statement?

How to implode data in array with commas from database column where has value = 1 in foreach statement?


By : Buồn
Date : March 29 2020, 07:55 AM
To fix the issue you can do You can solve this in different ways:
First: save it directly into an array: To achieve this you have to change $data[$no]['jadwal'] = "Monday"; to $data[$no]['jadwal'][] = "Monday";. Now the field jadwal inside the $data array is an array itself and not a string. The brackets are a short hand for array_push. So Monday will be pushed to the jadwal array. Repeat this change to the other lines as well and you get an array of days at the end, where the database field equals 1.
code :
$jadwal = ['monday', 'tuesday', 'wednesday', ...];
$jadwal = array_filter($jadwal, function($day) use($d) { return $d[$day]==1; });
Using if statement in array.forEach function statement to choose sub-object in array of objects

Using if statement in array.forEach function statement to choose sub-object in array of objects


By : user2216027
Date : March 29 2020, 07:55 AM
Any of those help 1) You have to use bracket notation to access properties as strings.
2) You have to close the brackets on if/else correctly.
code :
const arrofobj = [   
  {"thing_id":"1a", "val": 1, "Type": "Switch","ValType":{"0":"Open","1":"Closed"}},  
  {"thing_id":"1b", "val": 72, "Type": "Sensor","ValType":{"0":"%"}}
];
const getCat = function( obj ){
    if(obj.Type !== 'Switch') {
        return obj.ValType[ '0' ]
    } else {
        return obj.ValType[ obj.val ];
    }
};

arrofobj.forEach(p => {
  p.CatX = getCat(p);
}); 

console.log( arrofobj );
Im trying to output all the data from my database and there is an error in the foreach statement

Im trying to output all the data from my database and there is an error in the foreach statement


By : aceboy
Date : March 29 2020, 07:55 AM
will be helpful for those in need There are a few points here:
you aren't capturing the result of Select presumably your view doesn't have the correct model definition
code :
MovieViewModel[] movies = db.MoviesData.Select(movie => new MovieViewModel
{
    MovieID = movie.MovieID,
    MovieName = movie.MovieName,
    MovieDescription = movie.MovieDescription,
    MoviePrice = movie.MoviePrice,
    MovieCategory = movie.MovieCategory,
    MovieYear = movie.MovieYear
}).ToArray();

return View(movies);
@model MovieViewModel[]
Related Posts Related Posts :
  • how to get days of difference between two dates in php
  • How to get any column entry from another table of database based on the entry taken in form?
  • Cann't delete row in php using AJAX
  • Extract array with same name from multidimensional array
  • creating multiple rows based on record in an array - laravel
  • How to make an update in the Controller with a unique 'email' field in Laravel?
  • PHP: AdjacentElementsProduct - CodeFights https://app.codesignal.com/arcade/intro/level-2/
  • Codeigniter Check For Duplicate on Edit Function
  • PHP - Merge and Combine Array
  • Possible to combine regex for last word & number?
  • How to loop a REST API response?
  • How to load a lib with composer?
  • Query datetime field with only date in Laravel Eloquent
  • What is the right algorithm for digitally signing in C and veryfing in PHP?
  • Trouble sending image data to php file using ajax
  • PHP - preg_replace how to add an extra "]" and an extra "["
  • how to looping one data in laravel
  • Laravel Jobs (Queue) do not worked
  • PDO 'fetchAll' return rows with an extra array
  • How to Remove Duplicates Values from table
  • Post data and file together to Laravel API using Guzzle
  • How can I install imagick in current enabled PHP version?
  • Displaying japanese characters with PHP
  • SQLSTATE[HY000] [1045] Access denied for user 'mamim'@'localhost' (using password: YES)
  • How can I turn this string into a key value array?
  • PDO sql delete with multiple conditions
  • Call files, cancel call if webservice say so
  • PHP Download script error failed to load PDF document
  • Strange behavior reading DBF file in PHP using fread()
  • How to host or stream video uploaded in google drive using google drive api
  • How to get data from a string
  • adding wp store locator to woocommerce checkout
  • Can not silence a mysqli call
  • Refactor foreach with multilevel array
  • Catch exception in function, called inside try-catch. Doesn't works, why?
  • PHP is not removing files due to directory or files not being found
  • Laravel, how to sort and paginate of loop array
  • 3 tables many to many relationship
  • Show English characters instead of Arabic - WordPress
  • is there a way to reestructure file_get_contents output? php
  • mysqldump command is not executed just it is saved on a file
  • How to change the page URL with a button
  • Prevent duplicate entries in Log
  • Use variable inside another variable in a mail blade view
  • Login Redirect with router
  • Can you perform CRUD on CouchDB view?
  • Laravel, How to get array of session in controller and compare with model
  • Can't decode GET parameter
  • JSON decode foreach -> only delivery 1 row
  • Laravel/Ajax - Load more results without new results conflicting with the request
  • How to write in 2 input at the same time
  • Where to put Datatables JS logic in laravel using laravel mix?
  • product search by Category
  • Why silex don't resolve App in consructor?
  • append xml nodes inside another node in php
  • Events adding and loading problem in jQuery Full calendar using Sql Server and PHP
  • Detect 'false' with laravel @forelse and stop looping
  • php form not showing output?
  • How can i convert thai date to english using PHP?
  • How to handle duplicate entry for email error?
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org