build link using postgresql and php - php

)
I have a question:
I have a databse with the following information:
Name of report [name]
Link to report [link]
The various reports are displayed in tables in my page (using db_tables filed with static, month, week etc)
But i want to generate a link to the report based on "link" and "name"
so i set up a pgsql query :
result = select name, link from db where type = 'week'
This works fine.
To display the above i have:
echo "<div id=\"selMaand\">" ;
echo "<table id = \"t2\">\n ";
echo "\t<tr class=\"head\">\n";
echo "<th colspan=\"2\">Select Maand</th>";
while ($line = pg_fetch_array($result5, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td width=\"100%\">$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
echo "</div>";
which works fine to.
Now, the link from database is http://192.168.178.1:8080/etc/etc/page.html"onclick humptydumpty>name
each time i want to adjust something in either the onlclick or address, i have to change all my links.
What can i insert in my querys so i will be able to build a link like;
echo "<a href = "http://192.168.178.1:8080/etc/etc/**$link**"onlclick humptydumpyu>**$name**</a>
I cant figure out how to break down my array!
Thanks in advance
Sjoerd

If you just need a portion of your URL in the database (i.e. only a portion of it would change for each record), then only store that portion of the URL in the database. You can then generate the remaining portion of the URL (call it the URL base) in your code.
So something like this:
define('URL_BASE', 'http://some.url.base/that/you/can/change/globally/');
Then you get the remaining piece of the URL from the database for each item. So you would have something like this inside your query loop to build the full URL.
$link = $line['link'];
$final_url = URL_BASE . $link;
As far as the onclick stuff goes, I would have that set sepearately as well, since I assume the onclick behavior is not dependent on the individual link (if it is you can make a separate DB column for it).
define('LINK_ONCLICK', 'onclick="some_onclick_function()"');
And in your query loop you can put this together like this:
$name = $line['name'];
$link = $line['link'];
$final_url = URL_BASE . $link;
echo '<a href="' . $final_url . '" ' . LINK_ONCLICK . '>' . $name . '</a>';
Note that it is not really necessary to define the strings for the url base and onclick behavior as constants. I just showed it this way as it is common practice to set globally defined values that do not need to change at run-time ion such a manner.

Related

How do I get custom PHP to use existing CSS styling in Wordpress

I am using woody snippets to insert php code to make an API call to return information in a table. How do I get the table to display using the default css from my wordpress theme?
I am new to PHP, but not to wordpress. And I know just enough CSS to get myself in trouble. So, I need ELI5 instructions.
My programmer provided the basic PHP code to call the API and return the data in a basic table. But, it looks horrid and there's no spacing or styling.
$tableStart = "<table>";
$tableEnd = "</table>";
$trStart = "<tr>";
$trEnd = "</tr>";
$tdStart = "<td >";
$tdEnd = "</td>";
$fname = 'alabama_energy_data.json';
$data = json_decode(file_get_contents($fname));
echo $tableStart;
foreach($data as $bill_data)
{
echo $trStart;
echo $tdStart . $bill_data->bill_id . $tdEnd . $tdStart . $bill_data->title . $tdEnd;
echo $trEnd;
}
echo $tableEnd;
This returns a basic table with the data I need, but it's not styled. How do I get it to use the default CSS from our wordpress site that it's displaying in so that the table renders in a format that looks decent?
If your table isn't styled like it should be, then you're probably missing and id or class attribute at your table. You could check on what current attributes your "wordpress" tables have, and just add them to your table.
Example:
<table class="wp-class-table">
# OR
<table id="wp-id-table">
And one other thing, I personally don't really like the way you "echo" your data. If you want, you can read this answer. It shows a nicer way to do it in my opinion.

How to get the Text Value of a Clicked Link using PHP

I know it's very easy to get the text value of a link using jQuery.
But Isn't it possible to get the text value using only PHP? Please have a look at my code:
echo '<table>'
foreach($array['data']['results']['titles'] as $data) {
$title = $data['title'];
$id= $data['id'];
$url = $data['url'];
echo '<tr>';
echo '<td>' . ''.$title.'' . '</td>';
echo '<td>' . '<a href= ' .$url . ' target="_blank" >IMDb Link</a>' . '</td>';
echo '</tr>';
}
echo '</table>';
This picture shows the output of my code
Suppose the user clicked on the Third movie - Batman: The Animated Series. How do I make my movie.php page look like this -- (image below)
The Link Text (which is $title) should be passed to the movie.php page and also the IMDb Link of the corresponding movie (which is stored in the $url variable)
The Only way I know is using $_SESSION but it won't work in this case as it will only store & pass the last value of the foreach loop
Please help me in this regard. Thanks :)
Considering $id as the row ID for the particular movie from the Database. You can use url encoding over here. Your code must be
echo '<td>' . ''.$title.'' . '</td>';
When clicked your URL will look something like.
movie.php?id=3
On your movie.php file use $id = urldecode($_GET['id']); to get the movie ID and you can fetch the relevant data from the DB again.
Let me know if you have any issues.
Try sending all info with Proper Primary key OR here is runtime solution
echo '<td>' . ''.$title.'' . '</td>';
Don't forget to urlencode the variables
each link should contain the unique filed like $id so your url should be echo '<td>' . ''.$title.'' . '</td>';
You can pass the encoded value of $id so end user cannot guess the value.In movie.php you decode the $id& show information on the basis of $id

facebook api get my friends profile pic

Hi there I am creating an app and I want to display my friends profile picture, but what happens in my case is that my profile picture is displayed x the number of the friends that I have. Any idea of what I should change on my code ? Thanks.
$uid = $facebook->$facebook->api('/me/friends/');
//create the url
$profile_pic = "http://graph.facebook.com/".$uid."/picture";
foreach($user_graph['data'] as $photokey => $photovalue){
echo "<img src=\"" . $profile_pic . "\" />";
}
From the code you have provided I see a couple of problems. Why are you calling $facebook->$facebook->api()? Surely that should be $facebook->api('/me/friends/');
Secondly, your api call returns an array, it looks like you are trying to treat $uid as a single value to me.
Also, If your loop works I imagine $user_graph must be defined elsewhere in your code.
I would probably just do something like this:
$friendlist = $facebook->api('/me/friends/');
foreach($friendlist["data"] as $value) {
echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
}
It's not going to look very neat but it should yield the result you were originally going for.

Giving ids to nested xml data attributes, in php

I've got a data feed I'm importing that has a load of 'markets', I want to have a main page displaying all the markets, so for that id use a foreach loop to go through the data and on each market make a listing.
Each market has a bunch of attributes as well as nested participants, I want to then make a page for each market, that displays some information about each participant.
So the user would navigate to index.php > event.php?id101
This is the bit were ive become stuck, how can i send the user to the right page, I was thinking of using
<?php
$xml = simplexml_load_file('f1_feed.xml');
foreach($xml->response->williamhill->class->type->market as $event) {
$event_attributes = $event->attributes();
echo "<tr>";
// EVENT NAME WRAPPED IN LINK TO EVENT
echo "<td>";
echo '<a href="event.php?id=' . $market_id . '">';
echo $event_attributes['name'];
echo "</a>";
echo"</td>";
// DATE
echo "<td>";
echo $event_attributes['date'];
echo "</td>";
echo "</tr>";
}
?>
but how can I set a var $market_id (from the xml feed) to add to the end of the url, so it sends me to the right page ?
(f1_feed.xml is the same as the live xml feed, its just local for development)
the feed I'm using is http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N
which im bring in using simplexml
This worked for me;
$xml = simplexml_load_file("openbet_cdn.xml");
foreach($xml->response->williamhill->class->type->market as $market) {
// that gives an object (native)
$market_attributes = $market->attributes();
printf("%s\n",
$market_attributes->id,
$market_attributes->name);
// that gives an array (useless way!)
// $market_attributes = (array) $market->attributes();
// printf("%s\n",
// $market_attributes['#attributes']['id'],
// $market_attributes['#attributes']['name']);
}

return json string with html characters?

i want to call a php with jquery ajax to perform some database things and then return 2 lists of links. so i have to pass these two link lists back to jquery so it can display list 1 in the left side of the webpage and list 2 in the right side.
i created the lists in separate arrays that i send back to jquery with json_encode but i noticed that it escapes all the html characters.
<a>dog</a> became <a>dog<\/a>
so when i displayed the list in the html they werent links anymore.
how can i preserve the html codes in my returned arrays to jquery?
EDIT: is this the right way to go if you want to split up data from php so that jquery can display them in different locations in html?
// list 1
while($row = mysqli_fetch_assoc($saved_tags))
{
$display_saved_tags[] = "<a id='showtag' href='answer.php?id=" . $row['id'] . "'>" . $row['name'] . "</a><br />";
}
// list 2
while($row = mysqli_fetch_assoc($related_tags))
{
$display_related_tags[] = "<a id='showtag' href='answer.php?id=" . $row['id'] . "'>" . $row['name'] . "</a><br />";
}
// return lists to jquery
echo json_encode('display_saved_tags' => $display_saved_tags, 'display_related_tags' => $display_related_tags));
json_encode's escape characters directly conflict with HTML output. I have had the same issue but decided to use an alternative solution at the time. I just had a thought that you could perhaps do this:
$data = new stdClass();
$data->html1 = base64_encode('<h1>html in here</h1>');
$data->html2 = base64_encode('<p><strong>more html</strong></p>');
echo json_encode($data);
On the frontend:
callbackFunction(json) {
alert(base64_decode(json.html1));
alert(base64_decode(json.html2));
}
You would need the javascript implementations of base64_decode and utf8_decode which can be found at:
http://phpjs.org/functions/base64_decode:357
use can use below function to un-escape the chars when reading or sending on to the browser:
html_entity_decode('your response here');
Also because your are using json_encode, make sure that you don't need the below function in your code:
json_decode

Categories