PHP highlight selected item - 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

Related

PHP Search Results - Retrieve Data based on ID in URL

I have a PHP search function which retrieves items from my database and displays them on a search results page. When clicking on a search result, it currently takes you to a separate html page (for each search item) which contains further details about the item.
I would like to link each search result to one PHP page which gets the item ID from the URL and then retrieves and displays the relevant data from the database.
Below is the PHP code from the page which displays the search results, but I am not sure where to edit this, to link each item to the dynamic PHP page and then retrieve the ID from the URL on the dynamic PHP page?
<?php
if (!empty($data)){
foreach ($data as $item){
echo '<div class="item">';
if (strlen($item['item_image']) > 10){
if(strlen($item['item_link']) > 10){
echo '<a href="'.$item['item_link'].'">';
}
else {
echo '<div class="fail ">No Results Found;
}
?>
Edit:
I have used the below code on the detail_page.php
<?php $db =
mysql_connect("","","") or die("Database Error");
mysql_select_db("items",$db); $id = $_GET['id']; $id = mysql_real_escape_string($id); $query = "SELECT * FROM `items-one` WHERE `id`='" . $id . "'"; $result = mysql_query($query);
But now need to call all of the row fields from the ID in the database and then add them at various points throughout the page?
Typically this is done by passing an id in a parameter via GET. So links on the listing page may look like this:
echo '' . $link_text . '';
Here $id and $link_text maybe be populated in loop or whatever.
On /path/to/detail_page.php page you would have some code like this:
// validate that there is an integer-like value passed in `$_GET['id']`
// if so, set value to $id
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
// see results of filtering and behave accordingly
if (is_null($id)) {
// $_GET['id'] was not set
// do something and exit
} else if (false === $id) {
// the value at $_GET['id'] didn't pass validation filter
// do something and exit
}
// $id has a good integer value
// note you would probably need additional validation checks on the id value
// i.e. make sure value is not negative or 0
// you may want to cast $id to int to make these checks
// for example:
$id = int($id);
if ($id < 1) {
// bad $id value
// do something and exit
}
// read data from DB and display it

how to add , edit and delete comma separated value of database .?

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
?>

Cycle through a list of items to ensure a different slug?

I know this has been done before. You can see an example of it whenever you post a new blog post/page in Wordpress, and the title is the same title as an existing page/post. Here's an example:
some-page-slug
some-page-slug-1
some-page-slug-2
How would you programmatically (using PHP) deal with someone submitting the slug of "some-page-slug" with the given list. Obviously, you should result in "some-page-slug-3", but what does that code look like? For some reason, this escapes me. I'm assuming, and hopefully I'm wrong, you would have to use jQuery (or vanilla js, whatever), correct?
Here's a possible solution like Mathew MacLeans suggested in his comment:
$slug = 'slug';
$slugs = array('slug', 'slug-1', 'slug-2', 'slug-5');
$result = $slug;
$i = 1;
while(in_array($result, $slugs)) {
$result = $slug . '-' . $i;
++$i;
}
// prints 'slug-3'
print $result;
Of course you have to replace in_array with your function that checks for existence of a slug.
Demo
Try before buy
Just some pseudo-code to get you going, but I believe this is the route you should take.
$postTitle = <WhateverTheTitleIs>;
$result = mysql_query("SELECT id FROM mytable WHERE title = '$postTitle'");
if(mysql_num_rows($result) == 0) {
// title not found, submit to database
} else {
// title exists
$postTitle = $postTitle + 1;
}
Now, obviously this isn't anywhere near 100% correct syntax, but it should more than point you in the direction that you need to go. :)

PHP - How can i select multiple check boxes and show all the values from the selected check boxes?

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.

Drupal & PHP/MySQL : How to compare fields with current user's field?

I've an issue, which is about php syntax/mysql in drupal:
Let's say that userA has created a content type called "test" where he filled the field field_example with value "xxx". Afterwards, another user, userB has created another content and filled the same field field_example with the same value "xxx".
I'd like to know how is it possible to display a view only with the node created where the field field_example is the same for the current user ? I don't have (and i don't want) a user reference in the content type "test" i'm using.
I've looked through View PHP Filter, but i'm wondering how to compare values of field ? Here's my attempt [i'm not an expert in PHP as you'll might notice :) ] :
<?php
$a="SELECT uid FROM users WHERE uid=%d";
/* ??? How to create $b which can get value of field_example from content_type_test from current user which is logged in ? */
$b="";
$c="SELECT field_example FROM content_type_test";
if ($b==$c){
echo "Ok, I've what I want :) ";
}
?>
Any help would be greatly appreciated since it's been a while i'm looking for information about this query...
Thanks all :)
I don't have a Views module solution.
One thought that comes to mind is what if User A submits multiple nodes, which example value would you use then? Also, what version of Drupal are you working with?
Assuming that a user will only ever submit one content of this type and you are running Drupal 6 (my guess from code examples), then it might look something like this:
<?php
// current user
global $user;
// select this user's node
$nid = db_result(db_query("SELECT nid FROM {node} WHERE type = 'my_content_type' AND status = 1 AND uid = %d", $user->uid));
// if this node loads fine, then proceed with the rest
if ($node = node_load($nid)) {
// find nodes with the same example value, which do not belong to the current user
$result = db_query("SELECT nid FROM {node}
INNER JOIN {content_type_test} ctt ON n.vid = ctt.vid
WHERE n.status = 1 AND ctt.field_example_value = '%s' AND uid <> %d
ORDER BY n.created DESC", $node->field_example[0]['value'], $user->uid);
// loop through results
while ($row = db_fetch_object($result)) {
$node = node_load($node->nid);
// append the teaser output (if this is what you want to do)
$output .= node_view($node, TRUE);
}
// print the output
print $output;
}
else {
print t('No other content found.');
}
?>
If users submit more than one of those content types, then that would have to be another answer to avoid from writing a novel here. There's a couple ways to approach that.
Also if this was Drupal 7, I'd be using different functions as well.
I assume you want something like this:
function _get_list() {
global $user; // This is global variable, contains information for current logged in user.
$results = db_query("SELECT ctt.field_example FROM {node} n JOIN {content_type_test} ctt ON n.nid = ctt.nid AND n.vid = ctt.vid WHERE n.uid = %d", $user->uid);
while($row = db_fetch_object($results)){
//$row->nid.. etc..
}
}

Categories