Displaying bread crumbs array keyed to number of segments - pdo

I just converted a query that displays "bread crumbs" style navigation links to PDO:
function get_path($dbh,$node,$TopnavTable, $TopnavName) {
$stmt = $dbh->prepare("SELECT name FROM $TopnavTable WHERE $TopnavName = ?");
$stmt->bindValue(1,$node);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$row = $stmt->fetch();
$path = array_merge(get_path($pdo,$row['Parent'], $TopnavTable, $TopnavName), $path);
return $path ;
}
I also figured out how to display the results:
$Path2 = explode("/", $path);
$Path2[1] = ''.$Path2[1].'';
$Path2[2] = ''.$Path2[2].'';
$Path2[3] = '<span class="navHere"><b>'.$Path2[3].'</b></span>';
echo join( $Path2, ' > ' );
But there's a catch. The above works only if I'm working with an array consisting of three segments. For example, I'm viewing the URL MySite/Topics/Washington/Governor, which displays the following bread crumbs trail:
Topics > Washington > Governor
If I view MySite/Washington, it should display...
Topics > Washington
But I get an error message: Undefined offset: 3
So I'm trying to figure out how to make this work with any number of segments - 2, 3, 6, etc. Regardless of the number of segments, I'd like the last segment to be unlinked. (I'm going to further put it inside a span.) Does anyone have any tips?

When $path has only 3 members your code works. You have to modify your code to suit if different using array function count() & then loop through array.
$path ="Topics/Washington/Governor/Trash";
$Path2 = explode("/", $path);
$arrlength=count($Path2);
$html =''.$Path2[0].' > ';//First Member
for($x=1;$x<$arrlength-1;$x++) {//Between first & last
$html .= ''.$Path2[$x].' > ' ;
}
$html .= '<span class="navHere"><b>'.$Path2[$x].'</b></span>';//Last member
echo $html ;
RESULT for 3
Topics > Washington > Governor
for 2
Topics > Washington

Related

duplication of products in prestashop backend and front

I have imported 16 000 products, and now I have 57 000 products.
Can any one help me please to delete all duplicated products as you see in picture.
Product with different id and same reference.
Image backoffice:
Image front:
You can try something like that, you can test it in development environment, you should create a php file in your root project.
require_once('config/config.inc.php');
require_once('init.php');
$query = "select id_product,reference from " . _DB_PREFIX_ . "product where active=1";
$res = Db::getInstance()->ExecuteS($query);
foreach($res as $prod){
$query = "select id_product from " . _DB_PREFIX_ . "product where reference=$prod['reference']";
$res = Db::getInstance()->ExecuteS($query);
$count = count($res);
if($count){
foreach ($res as $key => $p) {
if (--$count <= 0) {
// to not delete the last occurrence for a given reference
break;
}
$id_product = $p['id_product'];
$product = new Product((int)$id_product);
if($product->delete())
echo 'product '.$id_product.' is deleted';
}
}
}

How do i use JOIN with a get_where clause in CodeIgniter

I'm trying to get the data from two tables after an "Open" button is clicked from previous table UI by sending an Id.
e.g.
<a name="Open" href="<?php echo base_url('welcome/WellProfile/'.$post->Id) ?>">
(https://imgur.com/6IU1Tfo)
So I have two tables - namely "WellDataA" and "WellDataB" , I want to get the data where WellDataB data matches with the WellDataA by WellName(column).
Example:
WellDataA:
Id Platform WellName
.
.
4 ZE ZE-A
5 ZE ZE-B
6 ZE Ze-B
.
.
WellDataB:
Id WellName CompleteDate
1 ZE-A 12/3
2 ZE-B 14/5
3 ZE-C 20/6
This is how my query so far, but it ended up error
public function get_news_by_id($Id = 0)
{
if ($Id === 0)
{
$query = $this->db->get('WellDataA');
return $query->result_array();
}
$this->db->select('*');
$this->db->from('WellDataA');
$this->db->join('WellDataB','WellDataA.WellName =
WellDataB.WellName','INNER');
$query = $this->db->get_where('WellDataA', array('Id' => $Id));
return $query->row_array();
}
I expect the output would show ZE, ZE-A, 12/3 when "Open" button is clicked on ZE-A. But the actual output is ZE, ZE-A only. Thank you so much in advance!:)
Try this, your issue is in get_where() so, i removed it and rewrite it
public function get_news_by_id($Id = 0)
{
if ($Id === 0)
{
$query = $this->db->get('WellDataA');
return $query->result_array();
}
$this->db->select('a.Id,a.Platform,a.WellName,b.Id,b.CompleteDate');
$this->db->from('WellDataA a');
$this->db->join('WellDataB b','a.WellName = b.WellName','INNER'); //changes
$this->db->where('b.Id',$Id); //changes
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row_array();
}else{
return array();
}
}
Firstful I am sorry if my solution won't be a good one,
but I suggest you change the structure of your tables to this:
WellDataA:
Id Platform WellDataB
.
.
4 ZE 1
5 ZE 2
6 ZE 3
.
.
WellDataB:
Id WellName CompleteDate
1 ZE-A 12/3
2 ZE-B 14/5
3 ZE-C 20/6
Since I assume that the used model is relational, one thing must first achieved is to make the tables all relational. Instead of connecting WellDataA and WellDataB with the Id from WellDataB inside WellDataA.
I think you just need to remove some code. Just try this code
public function get_news_by_id($Id = 0)
{
if ($Id === 0)
{
$query = $this->db->get('WellDataA');
return $query->result_array();
}
$this->db->join('WellDataB','WellDataA.WellName =
WellDataB.WellName','INNER');
$query = $this->db->get_where('WellDataA', array('Id' => $Id));
return $query->row_array();
}

How do I do different maxlengths for different text fields?

I have an options panel in my WordPress theme with several text fields e.g. project title, project description, etc.
case 'text':
$val = $value['std'];
$std = get_option($value['id']);
if ( $std != "") { $val = $std; }
$output .= '<input class="" maxlength="12" name=""'. $value['id'] .'" id="'. $value['id'] .'" type="'. $value['type'] .'" value="'. $val .'" />';
break;
As the above code shows... I currently have my max length for the text fields as 12, but I would like to have a max length to be different for each of the text fields. How can I do this?
Define an array containing the fields and their corresponding maxlength values.
(I've used field names, but you could also use an Id number)
E.g: field_name => max_length_for_that_field
$maxFieldLengths = array(
'project_title' => 12, //project title has 12 max length
'project_description' => 100 //project description has 100 max length
);
Identify which text field (input) you are currently building and use that identifier to reference your maxFieldLengths array.
case 'text':
...
$field_name = // Identify the current field here;
$output .= '<input class="" maxlength="'. $maxFieldLengths[$field_name] .'" etc .....' />
break;
Note: From your code sample it's not 100% clear how you identify the field you are currently building. You could, for example, use something like:
...
$field_name = getFieldFromId($value['id']);

getting the id from a table in database

i have 3 tables city, information and state. In my code i've shown the different cites under different state. what i want to do is to get the seperate ids of the cities so that i can run some queries in my 'information' table.
My model is given below:
function get_status(){
$states = $this->db->get('state');
$like = array();
$status = array();
foreach ($states->result() as $state){
$cities = $this->db->get_where('city', array('state_id'=> $state->id));
$like=$cities->result();
$status=$like[0]->city_id;
}
echo $status;
}
but when i run the code i only get the last id of the last city stored under the last state in the the table. for example the state 3 has city that has id 3. i'm only getting this id. but i want all the ids of the cities like- 1,2,3.
i'm using Codeigniter framework.
Thanks in advance
$like - it`s an array.
[0] - the first element of an array.
echo $like[0]->city_id;
I haven`t tested it. (In one iteration -> it takes all the cities from that ID)
function get_status(){
$states = $this->db->get('state');
$like = array();
$status = array();
foreach ($states->result() as $state){
$cities = $this->db->get_where('city', array('state_id'=> $state->id));
foreach ($cities->result() as &$v){
echo $v->city_id."<br/>";
}
echo "......<br/>";
}
}

How can I tell if I'm at the last result when using WHILE so that I can omit a comma from my output?

I know I can do what I need to do by getting a total records count and if I'm at the last record, don't display a comma but there has to be a better way.
I'm trying to build an SQL statement programatically using values from MySQL.
The code:
$fql="SELECT ";
$result = mysql_query("SELECT field FROM fb_aa_fields WHERE fql_table = '$query'", $conn);
while ($row = mysql_fetch_array($result)){
$get_field = "".$row{'field'}."";
$fql = $fql."$get_field, ";
}
$fql = $fql."FROM ".$query." WHERE owner=".$get_uid."";
It outputs this:
SELECT aid, can_upload, cover_object_id, cover_pid, created, description, edit_link, link, location, modified, modified_major, name, object_id, owner, photo_count, size, type, video_count, visible, FROM album WHERE owner=522862206
The problem is the last comma between "visible" and "FROM". How would you suggest is the best way to make that comma go away?
It's less of a pain to detect whether you're at the first element than the last. You could do like
$i = 0;
while($row =...) {
if ($i++) $fql .= ',';
$fql .= $row['field'];
}
Or, possibly better, defer tacking on fields to the string til the end. There's a built-in function called implode, that you can use to insert the commas between them.
$fields = array();
while($row =...) {
$fields[] = $row['field'];
}
$fql .= implode(',', $fields);