I have a list file(buy.txt). It contains some lists of words/items. For example,"Book","Table","Fan".
I want to iterate through the list,and match the words of that list with my mysql table and select the data according to the list item from the mysql data. So,if I iterate,I should get all data of three items "Book","Table","Fan" from the mysql database. I get the data. But I get data of only one word/item from the list. For example,instead of "Book","Table","Fan",I get only "Fan" data from mysql table. Other two don't show up.
Looks like,I am not iterating the list file correctly. Can anyone help to get all data(i.e "Book","Table","Fan") not just one from the mysql....?
Looks like,I am not iterating the list file correctly. Can anyone help to get all data(i.e "Book","Table","Fan") not just one from the mysql....?
.And the 'buy.txt' file contains lists like this order :
Book
Table
Fan
Here is the php code...... :
<?php
$server='localhost';
$user='root';
$pass='123456';
$database='stock';
$all = array(); //let's store the data here
$link=mysqli_connect($server,$user, $pass, $database);
$file = "buy.txt";
$contents = file_get_contents($file);
$lines = explode("\n", $contents);
foreach($lines as $symbol) {
$sql= "SELECT * FROM `test` WHERE `price` = '$symbol'";
$data=mysqli_query($link,$sql);
$all[] = mysqli_fetch_array($data);
}
}
}
//now $all is an array and it contains all the infos, lets check it
print "<xmp>"; //<xmp> tag beautifies the output
print_r($all);
print "</xmp>";
?>
The output is :
Array
(
[0] =>
[1] =>
[2] =>
)
Thank you.
Related
I've started to code in April 2021 so I'm super new to this. Working on my final exam, I'd need your precious help! I want to generate a dynamic list from a database, containing 2 joined tables (users and interest). The 2 tables both contain "ID_user" which links them as a foreign key. The idea is that one the user is logged in, the profile page displays all the interests the user selected at sign up. At the moment I can only display the last interest selected and not all of them.
Here is my php:
$request2 = "SELECT `interest`.`name_Interest`
FROM `interest`
INNER JOIN `users`
ON `interest`.`ID_user` = `users`.`ID_user`
WHERE `pseudo` = '".$url_pseudo."'
";
$resultat2 = mysqli_query($connexion, $request2) or die (mysqli_error($connexion));
$nb_resultat2 = mysqli_num_rows($resultat2);
if ($nb_resultat2 > 0) {
while ($row = mysqli_fetch_array($resultat2)){
$name_Interest = $row["name_Interest"];
}
}
Here is the HTML displaying the response:
enter image description here
Here is my db:
enter image description here
Any idea why I can get only 1 value?
enter image description here
thanks in advance
Your while loop is writing to the same variable for each iteration of the loop;
while ($row = mysqli_fetch_array($resultat2)){
$name_Interest = $row["name_Interest"];
}
This will leave $name_Interest containing the last value from your database after the loop has completed.
To resolve this, you will need to keep a list of interest names - this can be achieved by using an array. PHP Array Documentation
// Declares the empty array
$interests = [];
// Loop through database results
while ($row = mysqli_fetch_array($resultat2)){
// Add this value to the array
$interests[] = $row["name_Interest"];
}
Now, $interests will hold all of the values from the database!
You will need to print these out differently in your HTML, by looping through all the values in the array and printing one at a time:
(PHP foreach documentation)
<ul>
<?php foreach ($interests as $interest) { ?>
<li><?php echo $interest; ?></li>
<?php } ?>
</ul>
Solution
Simple store all user interests on array and then show in iteration on page.
Code:
$user_interests=array();
while ($row = mysqli_fetch_array($resultat2)){
$user_interests[] = $row["name_Interest"];
}
Now $user_interests array holds all interests of users as a result of join.
At last loop over it
<?php foreach ($user_interests as $interest) { ?>
<p><?php echo $interest; ?></p>
<?php } ?>
Using a PHP script, I am pulling data from a database. In the column physicalDeliveryOfficeName is an array, as the values in one column are listed: FacultyActive|StudentArchived.
If anyone can point me in the right code direction to returning these results, I would be super appreciative! Right now, obviously, I'm only returning Array for this column.
I'll take any help even it is "look over here moron!"
Thanks in advance!
See code below
for ($i=0; $i<$info["count"]; $i++)
{
if(#$info[$i]['mail']['0'])
{
$name = $info[$i]['cn']['0'];
$email = $info[$i]['mail']['0'];
$firstname = $info[$i]['givenname']['0'];
$lastname = $info[$i]['sn']['0'];
$department = $info[$i]['department']['0'];
$title = $info[$i]['title']['0'];
$physicalDeliveryOfficeName[] = array(
"status" => $info[$i]['physicalDeliveryOfficeName']['0']
);
//write data to see what will appear in csv
echo $firstname.",".$lastname.",".$email.",".$physicalDeliveryOfficeName."<br />";
//$data element below writes to csv
$data = $firstname.",".$lastname.",".$email.",".$physicalDeliveryOfficeName."\n" ; ////// the complied line that gets written to csv file
write_email($data);
}
}
In both the display results page, a CSV column for physicalDeliveryOfficeName I would like to see this data FacultyActive|StudentArchived in that column
I running a simple mysql query and trying to grab from the src column but I only want to grab the "video#.mp4" part - not the whole value (see picture attached below)
If anyone knows how to achieve this... I really appreciated it! Thank you for any help!
Code:
$con=mysqli_connect("localhost","test","123","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT id, title, src, type, poster_img FROM TABLE_NAME";
if ($result=mysqli_query($con,$sql))
{
$id = 0;
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
// Show video#.mp4 here ///
echo '';
$id++;
}
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
I suggest you to store quality and video name in 2 different columns, because this will help you in searching etc.
In your example, you can achieve your desired result by using json_decode but again, it will work if you have valid json in src column.
Example:
$string = '[{"quality":"default","mp4":"video2.mp4"}]';
$array = json_decode($string,true);
print_r($array);
Result:
Array ( [0] => Array ( [quality] => default [mp4] => video2.mp4 ) )
Now you can get the index mp4 easily, if you have multiple file format, than you can group than inside your loop.
I'm developing an app about books and I had a table for contents. before, I made a mistake and I split content in myself and insert them in database. after inserting 90 books I had 1000 records and I found out that it was wrong. Now, I create a table of content and I insert all content of every books(every book has a record), so I want to split content with character(*).
but it doesn't work.
It is my Previous code:
$sql2 = "SELECT tblcontent.content
from tblcontent,tblstory
where tblcontent.storyid=tblstory.storyid
and tblcontent.storyid='$storyid'";
$r2 = #mysqli_query($dbLink,$sql2);
$result2 = array();
while($res2 = #mysqli_fetch_assoc($r2))
{
$result2[] = $res2;
}
and I've added this code:
$result2=explode("*",$result2);
and
if(isset($result2)){
echo json_encode(array("result2"=>$result2));
}
But it doesn't work.Thanks in advance
I have a big problem on saving multiple rows with multiple arrays into MYSQL. For example row 1 contains "name" and "share percentage". Then they add another 2 rows which contains same attributes as mentioned. So how do I save these data into DB. Below was my unsuccessful code:
foreach($_POST['name_members'] as $dir){ // array 1
$directorID = run_num('director_id','proc_director'); // generate running number for each row
foreach($_POST['share_percentage'] as $share) { //array 2
$insDirector = "INSERT INTO
proc_director(director_id, vendor_cd, director_name, director_percentage)
VALUES
('$directorID','$vendorID','".trim(addslashes($dir))."','$share')";
$db->query($insDirector); // save the array value into DB
}
}
I made demo interface, so that you can get the picture what I want. Here the hyperlink: http://softboxkid.com/blog/code/add_row/
Thank for your respond. I already found the solution for my problem. Here is my code:
/* save partnerhip information */
$count_director = count(array_trim($_POST['name_members']));
for($i=0; $i<$count_director; $i++) {
$directorID[] = run_num('director_id','proc_director'); // generate running number for each row
$insDirector = "INSERT INTO proc_director(director_id, vendor_cd, director_name, director_percentage)
VALUES('".$directorID[$i]."','".$vendorID."','".$_POST['name_members'][$i]."','".intval($_POST['share_percentage'][$i])."')";
$db->query($insDirector); // save the array value into DB
}