@Mittineague sorry for posting it her but it started of as a database question, should I continue there? Anyway. I i am not ure what you mean with:
How do I make conditional checks?
@Mittineague sorry for posting it her but it started of as a database question, should I continue there? Anyway. I i am not ure what you mean with:
How do I make conditional checks?
Try temporarily changing your code to this
<?php foreach ($details as $detail){
var_dump($detail);
echo '<br>';
// echo $detail['heading'];
// more variables
} ?>
Does that let you see whatās in the array the database query is returning?
@Mittineague Thanks for moving it over to here.
The var dump is returning the right content but it looks like things are double. I donāt know how to read this This is the first of the three blocks:
array(32) { ["widget_id"]=> string(1) "1" [0]=> string(1) "1" ["dashboard_id"]=> string(1) "4" [1]=> string(1) "4" ["query_string"]=> string(7) "widgets" [2]=> string(7) "widgets" ["url_string"]=> string(13) "opening_dates" [3]=> string(13) "opening_dates" ["widget_name"]=> string(13) "Opening dates" [4]=> string(13) "Opening dates" ["related_photos"]=> string(1) "2" [5]=> string(1) "2" ["isActive"]=> string(1) "1" [6]=> string(1) "1" ["content_id"]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(13) "opening_dates" ["heading"]=> string(13) "Spring season" [10]=> string(13) "Spring season" ["sub_heading"]=> NULL [11]=> NULL ["field_1"]=> NULL [12]=> NULL ["field_2"]=> NULL [13]=> NULL ["content"]=> string(54) "This spring we will be open from March 15 till June 15" [14]=> string(54) "This spring we will be open from March 15 till June 15" ["animation_delay"]=> string(4) "1.4s" [15]=> string(4) "1.4s" ["photo"]=> string(26) "flying_paradise_spring.jpg" [16]=> string(26) "flying_paradise_spring.jpg" }
Try surrounding var_dump($variable); with echo '<pre>';
and echo pre('/pre>';
and the output will be more readable because line feeds are added.
@John_Betong Thank you for the advice
This is the first block from the three:
array(32) {
["widget_id"]=>
string(1) "1"
[0]=>
string(1) "1"
["dashboard_id"]=>
string(1) "4"
[1]=>
string(1) "4"
["query_string"]=>
string(7) "widgets"
[2]=>
string(7) "widgets"
["url_string"]=>
string(13) "opening_dates"
[3]=>
string(13) "opening_dates"
["widget_name"]=>
string(13) "Opening dates"
[4]=>
string(13) "Opening dates"
["related_photos"]=>
string(1) "2"
[5]=>
string(1) "2"
["isActive"]=>
string(1) "1"
[6]=>
string(1) "1"
["content_id"]=>
string(1) "1"
[7]=>
string(1) "1"
[8]=>
string(1) "1"
[9]=>
string(13) "opening_dates"
["heading"]=>
string(13) "Spring season"
[10]=>
string(13) "Spring season"
["sub_heading"]=>
NULL
[11]=>
NULL
["field_1"]=>
NULL
[12]=>
NULL
["field_2"]=>
NULL
[13]=>
NULL
["content"]=>
string(54) "This spring we will be open from March 15 till June 15"
[14]=>
string(54) "This spring we will be open from March 15 till June 15"
["animation_delay"]=>
string(4) "1.4s"
[15]=>
string(4) "1.4s"
["photo"]=>
string(26) "flying_paradise_spring.jpg"
[16]=>
string(26) "flying_paradise_spring.jpg"
}
Not sure how to read this but it looks messed up. The content is right but a lot of double content
To limit the number of rows being displayed try this slightly modified script.
<?php for each($details as $id => $detail) {
if($id > 3){
die; // prevent further iterations
}
var_dump($detail);
}
Thatās what you asked PHP to do with fetch all. (perhaps it would be better named fetch both).
I more often use fetch associative than I use fetch numeric, but I canāt recall ever needing both. A main reason I prefer associative is because I can look at something like āuser_idā and know at a glance what it is, but it takes some effort to know that ā3ā is the user id.
In any case, if you truly need both, you could wrap the assign lines in conditional checks. eg. instead of
<?php foreach ($details as $detail) {
echo $detail['heading'];
// more variables
} ?>
which will choke on the numerical keys, you can do something like
<?php foreach ($details as $detail) {
if (isset($detail['heading'])) {
echo $detail['heading'];
}
// more variables
} ?>
to skip over the numeric keys.
Of maybe this could work
<?php foreach ($details as $key => $detail) {
if (!is_numeric($key)) {
echo $detail['heading'];
}
// more variables
} ?>
Information overload
Try replacing var_dump(ā¦); with print_r(ā¦);
@Mittineague I donāt need the numeric keys. I just used them to link the tables. This i the query I used:
public function get_widget_details($string)
{
$sql = "SELECT SW.widget_name
, WC.*
, WP.photo
FROM site_widgets SW
LEFT
JOIN widget_content WC
ON WC.widget_id = SW.widget_id
LEFT
JOIN widget_photos WP
ON WP.rel_id - WC.content_id
WHERE SW.url_string = ?";
$stmt = $this->pdo->prepare($sql);
$stmt->execute(array($string));
return $stmt->fetchAll();
I took out all fields from site_widgets except for the name
To be clear, you are not using them to JOIN the tables.
The problem is the fetchAll without specifying a fetch style. That tells PHP to return both numeric and associative keys (the default is fetch both). If you donāt need the numeric keys, try
return $stmt->fetchAll(PDO::FETCH_ASSOC);
You could also set the DEFAULT_FETCH_MODE
in your connection so that it will use a certain mode as @Mittineague is pointing out. I usually set it to fetch objects because I am an object oriented person. You may want to set yours to fetch associative arrays. Again, this should remove the unneeded keys.
@Mittineague That is what I just did, and it is working great, Thank you for the patience
Edit: And thank you for the links as well
@spaceshiptrooper. Thanks for the reply. How do I do that? setting it in my connection?
Yes. You set the DEFAULT_FETCH_MODE
into your database connection. This should remove the unneeded keys.
@spaceshiptrooper Thanks for the reply. This is new for me, I never done this before. This is my connection:
$this->connection = new PDO("mysql:host=" .$config['hostname']. ";dbname=" .$config['dbname'].";charset=utf8", $config['username'], $config['password']);
So lets say I would like to set PDO::FETCH_ASSOC as default as @Mittineague suggested, how should I do that?
Didnāt you try a research by yourself? Keywords are pdo DEFAULT_FETCH_MODE
, second entry.
To be fair, the documentation isnāt redundant so one needs to more or less look at everything to piece it together. In this case itās the options array passed to __construct.
public PDO::__construct ( string $dsn [, string $username [, string $passwd [, array $options ]]] )
where $options is
A key=>value array of driver-specific connection options.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.