PHP snippet always shows in the top of page in wordpress - php

I'm currently using a database search in wordpress wherein I show the results in the result page via snippets.
The problem is the results always get displayed in the top of page. I do understand php executes first before html, but is there a way to get the results displayed where I want?
Code:
Wordpress: [xyz-ips snippet="industry"]
Php:
$com=$_GET['company'];
$sql2 = "SELECT count(1) i from data where sector= (SELECT sector from data where company like '$com%')";
$rows2 = $wpdb->get_results($sql2 );
echo ' '.$rows2[0]->i.' ';

Long story short, you need to find where in WordPress the string of HTML is being output.
Then, you move it to where you want the HTML to be output.
The general idea, as you said, is that PHP executes (echo $html) before your browser reads the HTML sent by PHP. Your PHP script will run in order, top to bottom, so by moving sections of PHP that output HTML (or anything) to a different part of your script, you change its location on the page.
It's hard to give further input without understanding your situation in detail. I would highly recommend browsing the documentation for your "WordPress search" to see if there is a best practice for accomplishing what you want.

Thanks to the intelligent community here, they are more curious to downvote than to help fellow mate.!
I discovered after much search that the php version caused this. I downgraded (from the current 5.6) to 5.5 and that solved the issue.
This might help someone who is searching for a similar answer.

Related

Edit Script Output with Stylesheet

How are you?
I have a problem with the output of an internal webpage I use to print a query. It is ultimately just a formatting problem with the output.
The actual script (get_data.php) should do nothing more than to create an output for a certain topic or for a certain category (for example, records with the value "DVD" or books of an author).
That's exactly what it's supposed to do.
Now I have the problem that when I create the output this is not displayed correctly.
The actual text block sits one line below the bullet point. This is not earth-shattering but it does not look quite so pretty, so I would like to fix it. ;)
The website uses some stylesheets to give it their formatting, but the bullets or the list are generated by script. But I'm standing in front of the mountain and can not continue with this "formatting problem".
Can you help me?
Many thanks in advance
Dennis
PS: I hope my english isn't to bad.

Need advice about making a forum with php

I am trying to create a forum on my page. I need some advice. I have MySQL database and I am able to to work with it. Yet as far as I can make is playing with POST. I mean on click submit a $_POST['submit']) becomes set and so on. Then connect to database, get info and display.This way of making a forum is bad, because it all happens on a single page. As far as I seen any other forum when entering one and so on has it's unique Uri (what comes after domain.com/) so you can simply send someone full url and they are in that thread (unlike if you play with $_POST['submit']).
Sorry if my question is not accurate and I am not asking to write me code. I just need someone to direct me to the right place and any advice would be helpful. Thanks !
Your question is difficult to understand, but I think you are confused about how data and pages are dynamic using GET/POST.
The URLS with different IDs are like an illusion. Really, its the same page, however the page is dynamically taking that ID from the URL, and using it to output certain data.
For example, say we habe the URL: www.example.com/show_thread.php?thread=3
The php would look like
if(isset($_GET['thread'])){
$data = $db>query("SELECT thread_title,thread_text FROM threads WHERE thread_id = $_GET['thread']");
print($data);
}
*Warning: do not use this exact code, it is an example, not secure, etc
Then the database would look like
thread_id thread_title thread_text
1 kngwihywoihwy kngwihywoihwykngwihywoihwy
2 vyfngoieyoiehyon ieonuwrtoi hunwrmt jirwyji
3 nuoaiefguneoihn eoithneiotheo
and the page would show
nuoaiefguneoihn eoithneiotheo
POSTing data is for sending it to the server, and doing something with it. Just like GET. However, GET is helpful for passing things around as they are accessible in links. Like this example.

Running preg_replace on html code taking too long

At the risk of getting redirected to this answer (yes, I read it and spent the last 5 minutes laughing out loud at it), allow me to explain this issue, which is just one in a list of many.
My employer asked me to review a site written in PHP, using Smarty for templates and MySQL as the DBMS. It's currently running very slowly, taking up to 2 minutes (with a entirely white screen through it all, no less) to load completely.
Profiling the code with xdebug, I found a single preg_replace call that takes around 30 seconds to complete, which currently goes through all the HTML code and replaces each URL found to its SEO-friendly version. The moment it completes, it outputs all of the code to the browser. (As I said before, that's not the only issue -the code is rather old, and it shows-, but I'll focus on it for this question.)
Digging further into the code, I found that it currently looks through 1702 patterns with each appropriate match (both matches and replacements in equally-sized arrays), which would certainly account for the time it takes.
Code goes like this:
//This is just a call to a MySQL query which gets the relevant SEO-friendly URLs:
$seourls_data = $oSeoShared->getSeourls();
$url_masks = array();
$seourls = array();
foreach ($seourls_data as $seourl_data)
{
if ($seourl_data["url"])
{
$url_masks[] = "/([\"'\>\s]{1})".$site.str_replace("/", "\/", $seourl_data["url"])."([\#|\"'\s]{1})/";
$seourls[] = "$1".MAINSITE_URL.$seourl_data["seourl"]."$2";
}
}
//After filling both $url_masks and $seourls arrays, then the HTML is parsed:
$html_seo = preg_replace($url_masks, $seourls, $html);
//After it completes, $html_seo is simply echo'ed to the browser.
Now, I know the obvious answer to the problem is: don't parse HTML with a regexp. But then, how to solve this particular issue? My first attempt would probably be:
Load the (hopefully, well-formed) HTML into a DOMDocument, and then get each href attribute in each a tag, like so.
Go through each node, replacing the URL found for its appropriate match (which would probably mean using the previous regexps anyway, but on a much-reduced-size string)
???
Profit?
but I think it's most likely not the right way to solve the issue.
Any ideas or suggestions?
Thanks.
As your goal is to be SEO-friendly, using canonical tag in the target pages would tell the search engines to use your SEO-friendly urls, so you don't need to replace them in your code...
Oops ,That's really tough, bad strategy from the beginning , any way that's not your fault,
i have 2 suggestion:-
1-create a caching technique by smarty so , first HTML still generated in 2 min >
second HTMl just get from a static resource .
2- Don't Do what have to be done earlier later , so fix the system ,create a database migration that store the SEO url in a good format or generate it using titles or what ever, on my system i generate SEO links in this format ..
www.whatever.com/jobs/722/drupal-php-developer
where i use 722 as Id by parsing the url to get the right page content and (drupal-php-developer) is the title of the post or what ever
3 - ( which is not a suggestion) tell your client that project is not well engineered (if you truly believe so ) and need a re structure to boost performance .
run

Should I be using some sort of database?

In an attempt to redesign a website, I put this page together by hand. However, there are currently over 100 more shows already that need to be added, and three new shows get added every year.
In the old system, a text document was used as a makeshift 'database'; it stored data about each show, with shows separated by '#' and data fields separated with ']'. Here's an excerpt:
#The Whorl of the Leaves]WhorlOfTheLeaves]3]F]167
#Aladdin]Aladdin]8]N]0
#A Christmas Carol]XmasCarol84-]7]N]0
#The Feral Child]FeralChild]7]N]118
#Camelot]Camelot]11]N]169
A PHP script was then used to extract the information about each show and make the page seen here.
I'm sure a script could be used to put together a page such as the one shown above, but it seems like there should be a better system than a text document to store the information.
My question is: if the text document 'database' is working, should I bother changing it? Is there a better way to organize the information about each show?
SQLite would be prefect for you. It's a tiny database that requires no configuration or setup - yet comes built into most PHP installs.
Just use PDO and your good.
$db = new PDO('sqlite:/path/to/here/mydb.sq3');
$db->query('SELECT * FROM shows');
Yep, there are a ton of "better" ways of doing this. But if you're happy with it and it works, why change it?
You could save yourself a lot of trouble by using a content management system such as drupal.

sequentially show mysql data using php

I have a php code that fetches data from mysql. The data has (say) 15 rows. I want to display only 5 rows at a time to the user, with links to each of the set (3 in this case) such that when a user clicks on either of the links, the same page will show the corresponding results. Since, php code has the final result set, I don't want a solution that involves me to navigate to other pages and possibly re-calculate the next set of solutions (5~10 or 10~15). How can I do this? Thanks in advance.
If I am using javascript or ajax, how can I achieve this? I don't know javascript much.
You can try loading everything in your page and simulating the pagination thanks to javascript.
An example in jQuery here
It sounds like you want to send the Data as a complete set to the client but not let him display everything to the user. So use Javascript to just show you the pages 1*page_number to 5*page_number (with a for-loop).
By far, the easiest solution in this situation is a Dom-based Grid system. I recommend checking out Datatables. In essence, the Datatables code will take a fully-formatted html table and reformat it to include only the amount of rows you tell it to, with paging on the bottom as you've requested. In addition, you can turn on such features as filtering, toggle-able length, sorting, and selection. Once you get the hang of it, the additional code takes no more than a minute per table and the features are outstanding.
This is really straightforward. Use PHP to output ALL of the table, with all the rows. The only gotcha here is to include the full html, including <thead> and <tbody> Give the table an id such as "example"
Now, include the files that you download from the datatables site--datatables.js and Jquery.js. Instantiate the jquery like this:
$(document).ready(function() {
$('#example').dataTable();
} );
That's it. As you can see from the examples, it's a really cool tool. Good luck.

Categories