i have created one table called role and the fields are like (roleid,role,prohibitedprocess,prohibitedports) here roleid is unique. and i have a comma separated value for "prohibitedprocess" field .(ex: prohibitedprocess---> skype,teamviwer,notepad).
the problem is how to add and edit and delete that comma separated value of prohibitedprocess .is it possible ?
i have fetch the code form the data base .code is
<?php
$sql = "SELECT roleid, prohibitedprocess, prohibitedports FROM role WHERE role='Basic'";
$option='';
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
$pro_proc = $row['prohibitedprocess'];
$lst = explode(",",$pro_proc);
//Print in Option Box
foreach($lst as $exp)
{?>
<tr>
<td><?php echo $exp;?></td>
<td>Edit</td>
<td>Delete</td></tr>
<?php
}
}
?>
How to do this please help me
You can edit the whole string . .
I mean just using str_replace function
e.g
$string = "skype,teamviwer,notepad";
$old_value = "skype";
$new_value = "facebook";
$new_string = str_replace($old_value,$new_value,$string);
Then simply update this new string into database table. .
One way would be to retrieve all of 'keywords' in an input element and edit them like a sentence. However, for what you want to do, you may wanna add a new table called prohibitedprocess and use roleid as a foreign key. Then retrieve from that table according the roleid.
eg: SELECT item FROM prohibitedprocess p, role r WHERE p.roleid = r.roleid
It may be better to make another table for that relation, then you could edit it normally. Otherwise you can take your comma separated list and do something in javascript.
var commaSeparatedList = <?php echo $commaSeparatedList;?>
var realData = commaSeparatedList.split(',');
// when the user adds something you can add to the array or delete
// from the array
var newCommaList = readData.join([separator = ',']);
// some kind of get/post request that sends the new comma list
<?php
$newList = GET_REQUEST_PARAMS["newCommaList"];
// sql update query that updates the comma list
?>
I haven't used php in a while so I know GET_REQUEST_PARAMS isn't a thing but i'm sure you know how to get the parameters.
Or without javascript you can use a form that posts back all the names of prohibited things
<?php
$newList = ""
//foreach through the get params which contain the names
foreach()
{
$newList += getParam + ",";
}
// some code to remove that last comma
// sql update
?>
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 } ?>
Just like the image above, when the user clicks one of the list on the right side, it highlights the selected one.
$result = mysqli_query($con, "SELECT * FROM contacts") or die(mysqli_error($con));
while($row = mysqli_fetch_array($result)){
$company = $row['eyo_company_name'];
$id = $row['con_id'];
$editLinks .= "\n\t$company<br>";
}
this is how I brought the list out from the database. Would there be any way I could add a class or add b tag on selected one only ?
If I understand correctly, you are trying to highlight one of the links output by PHP. To do so you will need to know which one to highlight.
If you have a GET variable set to the current contact ID, you could do something like this:
while($row = mysqli_fetch_array($result)){
$company = $row['eyo_company_name'];
$id = $row['con_id'];
//let's check if we've pre-selected a company
// and if so, is this the company we selected?
if (true === array_key_exists('CURRENT_COMPANY_ID', $_GET) && $id === $_GET['CURRENT_COMPANY_ID']) {
//we did select this company for this PHP request
// let's make the HTML output custom to highlight the selected company
$editLinks .= "\n\t<strong>$company</strong><br>";
} else {
$editLinks .= "\n\t$company<br>";
}
}
This example expects that your request to the PHP script generating your list of links will include CURRENT_COMPANY_ID=<ID HERE> in the URL.
For example, if the URL for the request that generates your list of links looks like this:
/getMySuperAwesomeList.php
It would need to look like this:
/getMySuperAwesomeList.php?CURRENT_COMPANY_ID=123
Where 123 matches the con_id of the record you want to highlight.
I chose to use array_key_exists() to make sure that the request actually included the CURRENT_COMPANY_ID data in the URL. Here is documentation for that function: https://www.php.net/array_key_exists
I would like to create a file like this:
<?php
$cssMainVersion = "1.0.3";
$cssBootStrapVersion = "1.0.1";
$cssElseVersion = "1.0.4";
$jpgVersion = "1.0.1";
$jsMainVersion = "1.0.1";
$jsElseVersion = "1.0.1";
?>
I have created a mySQL table that holds the variable name, and the variable data. For example, the first row contains "cssMainVersion" and the second row contains "1.0.3".
How would I loop thorugh each row and print that data in a new file according to the format above?
You can create variables dynamically with php
$query = mysqli_query($conn, 'SELECT id, cachename, cacheversion FROM cache');
while($row = mysqli_fetch_assoc($query)) {
$$row['cachename'] = $row['cacheversion'];
}
the $$ will be evaluted from right to left, so, first $row['field'] will be replaced with this value, let say cssMainVersion, then we have $cssMainVersion
I am pretty new to PHP, but have tried searching for other questions similar to mine and been unable to find anything that is close enough to my situation to help me solve this.
I am trying to code a web page that allows users to select as many or as few items as they would like to order. The item values are identical to their Primary Key in the Item table.
Once submitted, each different item value should be input into the same row of a database table based on the date{pk}. Within that row, there are numerous columns: Item1ID, Item2ID, Item3ID, etc.
So far, the value of each item selected is assigned to a new array. However, I cannot simply input the array values into a column -- I need each array index to be placed into a sequential column. The code is below:
$date = new DateTime();
$td = $date->format('Y-m-d');
$x = 1;
$checkedItems = $_POST['Item'];
$count = count($checkedItems);
echo $count;
$foodID = "Item".$x."ID";
While($x<=$count){
if(isset($_POST['Item'])){
if (is_array($_POST['Item'])) {
foreach($_POST['Item'] as $values){
$selectedFoods = substr($values,0,4);
$addFoodOrderQuery= sprintf("UPDATE WeeklyBasketFoodOrder SET '%s' = %s WHERE `foodOrderDate` = '%s'",
$foodID, $selectedFoods, $td);
$result= mysqli_query($db, $addFoodOrderQuery);
}
}
} else {
$values = $_POST['Item'];
echo "You have not selected any items to order.";
}
$x++;
}
If you need any further clarification, please let me know. After submitting the code, the database item#ID tables are different, but they are now empty instead of "NULL."
I have one html table with all the values from a specific table, and i want to put an update button to update specific rows
And i want to update those rows using checkboxes.
The user just need to check the selectboxes and click at a button, and this button will display all the information from the rows that were selected.
I have here a bit of code that i created, but i just can read the last selected box value.
I'm using MVC structure, but with my own rules.
Here the model, I created the function inside the model:
$editjo_query = ("SELECT * FROM my_table");
$print_editjo = db_array($editjo_query, 'a+'); //this is a function that i created to select, ignore please
if(!empty($_POST['editcheck'])){
$editcheckbox = $_POST['editcheck'];
$countCheckedit = count($_POST['editcheck']);
for($i=0;$i<$countCheckedit;$i++) {
$edit_id = $editcheckbox[$i];
$editjo_query = ("SELECT * FROM my_table WHERE id=$edit_id");
$print_editjo = db_array($editjo_query, 'a+'); //this is a function that i created to select, ignore please
}
}
And this function is being called at the view
function editjoview($print_editjo){
foreach($print_editjo as $check){
echo $check['id'];
}
}
you are always writing to $print_editjo with $print_editjo = db_array($editjo_query, 'a+'); and you you do not process it. So in ever for loop you overwrite it, leaving you with the last entry.
Try this:
$print_editjo = array();
for($i=0;$i<$countCheckedit;$i++) {
$edit_id = $editcheckbox[$i];
$editjo_query = ("SELECT * FROM my_table WHERE id=$edit_id");
$print_editjo[] = db_array($editjo_query, 'a+');
}
and in your view, iterate over the array $print_editjo rather than just having $print_editjo as the last entry.
Note: If you would add how exactly the function in your view is called, the help could be more precise.