Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I would like to run a foreach loop with three functions inside of it but only the first function runs.
Here is my code
function import_events() {
global $wpdb;
$query_name = $wpdb->prefix . 'options';
$options = $wpdb->get_results("SELECT option_name, option_value FROM $query_name WHERE option_name LIKE 'event_url%'");
// array of option names
$i=1;
foreach ($options as $key => $row) {
$url = $row->option_value;
$city_name = $row->option_name;
import_venues($url);
import_organizers($url);
import_events_calendar($url);
}
echo"The Import Is Finished";
wp_die();
}
Any help is appreciated!
Based on your limited information there may be several possible causes to why the two remaining functions aren't running.
#1:
If your code outputs the "The Import Is Finished", then your functions are simply not doing what you ment for them to do. Perform more debug inside those functions.
#2:
If your code stops running at some point, it has probably stopped working due to a fatal error. Find the file named "error_log" (no extension) and see the entry with the most recent timestamp to determine if it may explain why your code stopped working.
#3:
Perhaps you're simply not seeing any errors. Since you're obviously working with WordPress, the error_log should end up in either /error_log or in /wp-admin/error_log.
While debugging, make sure to edit your /wp-config.php and define('WP_DEBUG', true) instead of false to enable error-logging.
If you're wanting to run functions you have in the same page which I'm assuming are due to the names you are using use these 3 lines to execute them within the for loop
$this->import_venues($url);
$this->import_organizers($url);
$this->import_events_calendar($url);
The problem ended up being in the function import_events_calendar($url)
The function was not returning anything which looked like it was not running but it really was just returning nothing.
Thanks for all the help!
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I receive the following error message when I attempt to call a function which I need to push an object into an array:
array_push() expects parameter 1 to be array, null given
Any clues why this is happening? Thank you in advance :)
<?php
$programming = array();
//some unrelated lines of code here inbetween
function createProgramming($data){
global $programming;
$prog = new Programming($data);
array_push($programming, $prog);
}
?>
//random HTML here
<php?
createProgramming("str");
?>
//more html
$programming is only referenced in the code at those three locations present in my extract above.
That code works fine. There are few things that could make it break:
$programming is redefined / unset before createProgramming() is called
$programming is not defined in the global scope
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 23 days ago.
Improve this question
I made a little script and now when I call the script the PDO should insert the row 1 time and not twice with an value that is not set.
This is the first code
function checkContent($content, $lang){
preg_match_all("/\[([^\]]*)\]/", $content, $matches);
$items = array();
foreach($matches[1] as $match){
$items[] = explode("|", $match);
}
foreach($items as $item):
$module = $item[0];
$slug = $item[1];
switch($module){
case "translation";
$content = str_replace("[{$module}|{$slug}]", translation($slug, $lang), $content);
break;
}
endforeach;
return $content;
}
In this code above it calls the function translation();
function translation($trans, $langTag){
global $cms_db;
$sql = $cms_db->prepare("SELECT translation FROM translations WHERE uniqueName = ? AND language = ?");
$sql->execute(array($trans,$langTag));
$translation = $sql->fetch(PDO::FETCH_ASSOC);
if(empty($translation)):
$sql = $cms_db->prepare("INSERT INTO translations (uniqueName, translation, language, new) VALUES (?,?,?,?)");
$sql->execute(array($trans,$trans,$langTag,'1'));
//EDIT, even with a die here, it enters 2 rows.
//die()
return $trans;
else:
return $translation['translation'];
endif;
}
In this code, is should enter 1 row in the database with the values.
$trans = "button.next";
$langTag = "NL";
Even if I use a die() in the function or not, it will insert two rows, one with the values good and the second one the $langTag is randomly changed to: favicon.ico.
The weirdest thing is that I am not using favicon.ico anywhere in my code.
I hope you guys can help me, maybe its me not looking good, but I get a little frustrated about it
Thanks in advance!
EDIT, NOT FIXED
The problem was that I forgot the meta favicon tag. Now it works fine, does someone can explain to me why the favicon is send to my script in the ?lang=XXX parameter?
EDIT 2
Too bad, I thought it was fixed, but today I got the values again in my database. This is really frustrated that it puts a random thing in my database
After a while of angry coding :P
I think I finally fixed it, I think it was the problem that I included some js/css that was not necessary and that was loading my script also.
Try to trace count of script calls per request writing to a log file (dont forget flock() it).
Obviously youre script is called twice for some reason, you should analyze access logs of http server too.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am facing some problem in my PHP function.
function pagename($id){
$query=mysql_query("select * from tbl_pages where recid='$id' and langid='$LangID'")
$rs=mysql_fetch_array($query);
$page_name=$rsp['pgname'];
print $page_name;
}
i am not getting any resutl
I saw some problems in your code
First, there is no connection string to your database, I hope you do the connection before to do the request.
Then, in your request, you try to use a variable $LangID not declared in your function, maybe you forgot to put it in your function declaration.
You put the result of your request in the $rs variable and then you try to read the $rsp variable.
You are using mysql in your code, actually it's very unsafe to use it, it's very recommended to use mysqli or PDO instead.
Finally, you don't return anything with your function, you are missing the return statement or maybe you just want to display the result ?
EDIT : I suggest you to write your SQL requests with uppercase, it's more readable for you and other people who read your code.
SELECT * FROM tbl_pages WHERE recid='$id' AND langid='$LangID'
There's no $LangID parameter.
$rs and $rsp are different variables in your code, you should use only one of them.
This function does not return anything, even if you get something in your print.
Check if mysql connection is already established.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hello All: I do not have much experience with Arrays but am trying to learn. Trying something new and don't know if this can be done this way or not. Essentially what I am trying to do is go through a wp database table; and go through the records; the $postnum value is equivalent to a WP post_id and is in the database multiple times. So I want to just whip through the table and create an array with each post_id ($postnum) in there just once. So I thought, all i need to do is create an array outside of loop; then while in loop just check if the $postnum is already in the array; if it is not just add it…so then all records with that same postnum in them they will not add to the array…
here is my code:
$posts_counted=array();
global $wpdb;
foreach( $wpdb->get_results("SELECT * FROM wp_c93hh3bk23_pvc_daily") as $key => $row) {
$id = $row->id;
$time = $row->time;
$postnum = $row->postnum;
$postcount = $row->postcount;
if (in_array($postnum,$posts_counted))
{
array_push($posts_counted,$postnum);
}
} // close out foreach
I have never really used the "in_array" thing or the "array_push" thing either; so these are all new to me…
I am getting: "Parse error: syntax error, unexpected ' ' (T_STRING) in…"
referring to the line "if (in_array($postnum,$posts_counted))"
So I am wondering if anyone can tell me if what I am trying to do can be done this way or what I am doing wrong on this…
Thanks!
This is how I get data from db:
$sql= "Select * FROM table";
$result=mysql_query($sql);
while($rows = mysql_fetchArray($result)){
$data= $rows;
}
//checking if something is in array
if(in_array($data['id')){
//do something
}
//now how you want to get data
// echo json_encode($data)
//OUTPUT: {id:"1",name:"xyz"}
Anyway if you want your "postNum" unique, fix your sql with adding DISTINCT after SELECT.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Ok got the code below working but it keeps displaying the q value at the top of each page. what do I need to change to stop this happening. I can see the echo value is that what the problem is if so what should I change it too in order to prevent value displaying? Many thanks.
// capture referral url
$referringPage = parse_url( $_SERVER['HTTP_REFERER'] );
if ( stristr( $referringPage['host'], 'google.' ) )
{
parse_str( $referringPage['query'], $queryVars );
echo $queryVars['q']; // This is the search term used
}
// general form data insert
$sql="INSERT INTO refer_kws (kwid, keyword, kwdate)
VALUES('','".$queryVars['q']."',now())";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "";
mysql_close($con)
remove this line:
echo $queryVars['q']; // This is the search term used
or turn it off by adding double slash at the beginning of the line like this:
// echo $queryVars['q']; // This is the search term used
First confirm that, do you want to display value or not. It seems that you have some misunderstanding regarding value insertion. you do not require to display value if you want to insert them in a database. You can remove whole line or make use of PHP comments 1. add // for one line comment 2. add /* at the starting of line and add */ at the end of line for multi line comments.