I want to know if there exists php treeview with data from mysql. I haven't found a suitalbe one for my project. Do you know if there is some plugins or code samples out there?
Thanks a lot.
Edit:
jQuery Treeview's asyncronous example, link text
I found it can work, but i don't know how to get the source.php. Do you have any ideas or other propositions?
you would need to run the query yourself, but it's pretty easy. the output the tree expects is an array of objects in json format like the example below.
your table structure could be:
tree_node (id, title, parent_id)
you would select the root node, then it's children, recursively until the tree is complete.
function expandTree($node)
{
$result = array('text' => $node['title'], 'children' => array());
$nodes = getChildren($node); // query all nodes whose parent_id = $node['id']
foreach ($nodes as $node) {
$result['children'][] = expandTree($node);
}
return $result;
}
output format:
[
{
"text": "1. Pre Lunch (120 min)",
"expanded": true,
"classes": "important",
"children":
[
{
"text": "1.1 The State of the Powerdome (30 min)"
},
{
"text": "1.2 The Future of jQuery (30 min)"
},
{
"text": "1.2 jQuery UI - A step to richnessy (60 min)"
}
]
},
{
"text": "2. Lunch (60 min)"
},
[...]
Assuming you have a db with parents and children, have a look at
http://www.ideashower.com/our_solutions/create-a-parent-child-array-structure-in-one-pass/ & http://www.phpriot.com/articles/nested-trees-1
Once you have your data correctly sorted, you can then look at rendering it.
To present bulk of data with parent child relationship Treeview is a classical approach. The major advantage of Treeview is using a Treeview we can show more data in less space. Assume that you have a global recruitment portal. You want to display job opportunities depending upon Countries and their Cities. In this case you required Treeview. Using a Treeview easily you can display Countries & related Cities. In this session let us share codes for a PHP Treeview using data from MySQL Database. In front-end using PHP I am binding data to ol li element of HTML. Then by applying CSS giving expand and collapse effects to the Treeview. Let us explain this PHP Treeview Example Step by Step. PHP Treeview Example using data from MySQL Database
Related
For an exercise/project in PHP, I have to program a route (with progression), but without being precise to the streets to be borrowed (as we know on GPS) but with lines between two points.
Example:
example route
The additional difficulty is to be able to display the progress on the line, starting from a percentage, the goal is therefore to have an image (like car, man or bicycle) on the straight line.
I have already worked with leaflet.js, but if another library is more appropriate I am taker.
I use this for the moment, for the points (Departure and arrival):
function placeMarkerDepartureArrival() {
// Departure
L.marker([varGPS[0].lat, varGPS[0].lng], {icon:myIconAD}).addTo(map);
// Arrival
L.marker([varGPS[1].lat, varGPS[1].lng], {icon:myIconAD}).addTo(map);
}
If you have any examples or site, I am taker.
Mapbox.js (based on the leaflet mapping library) has examples of animation and plotting lines on their documentation website, and has free tier for your exercise/project
Animating a point over a line
From the mapbox.js site:
map.addSource('point', {
"type": "geojson",
"data": pointOnCircle(0)
});
map.addLayer({
"id": "point",
"source": "point",
"type": "circle",
"paint": {
"circle-radius": 10,
"circle-color": "#007cbf"
}
});
function animateMarker(timestamp) {
// Update the data to a new position based on the animation timestamp. The
// divisor in the expression `timestamp / 1000` controls the animation speed.
map.getSource('point').setData(pointOnCircle(timestamp / 1000));
// Request the next frame of the animation.
requestAnimationFrame(animateMarker);
}
// Start the animation.
animateMarker(0);
Link to example
I'm currently trying to find all the pages where images/media from a particular category are being used on Wikimedia Commons.
Using the API, I can list all the images with no problem, but I'm struggling to make the query add in all the pages where the items are used.
Here is an example category with only two media images
https://commons.wikimedia.org/wiki/Category:Automobiles
Here is the API call I am using
https://commons.wikimedia.org/w/api.php?action=query&prop=images&format=json&generator=categorymembers&gcmtitle=Category%3AAutomobiles&gcmprop=title&gcmnamespace=6&gcmlimit=200&gcmsort=sortkey
The long term aim is to find all the pages the images from our collections appear on and then get all the tags from those pages about the images. We can then use this to enhance our archive of information about those images and hopefully used linked data to find relevant images we may not know about from DBpedia.
I might have to do two queries, first get the images then request info about each page, but I was hoping to do it all in one call.
Assuming that you don't need to recurse into subcategories, you can just use a prop=globalusage query with generator=categorymembers, e.g. like this:
https://commons.wikimedia.org/w/api.php?action=query&prop=globalusage&generator=categorymembers&gcmtitle=Category:Images_from_the_German_Federal_Archive&gcmtype=file&gcmlimit=200&continue=
The output, in JSON format, will looks something like this:
// ...snip...
"6197351": {
"pageid": 6197351,
"ns": 6,
"title": "File:-Bundesarchiv Bild 183-1987-1225-004, Schwerin, Thronsaal-demo.jpg",
"globalusage": [
{
"title": "Wikipedia:Fotowerkstatt/Archiv/2009/M\u00e4rz",
"wiki": "de.wikipedia.org",
"url": "https://de.wikipedia.org/wiki/Wikipedia:Fotowerkstatt/Archiv/2009/M%C3%A4rz"
}
]
},
"6428927": {
"pageid": 6428927,
"ns": 6,
"title": "File:-Fernsehstudio-Journalistengespraech-crop.jpg",
"globalusage": [
{
"title": "Kurt_von_Gleichen-Ru\u00dfwurm",
"wiki": "de.wikipedia.org",
"url": "https://de.wikipedia.org/wiki/Kurt_von_Gleichen-Ru%C3%9Fwurm"
},
{
"title": "Wikipedia:Fotowerkstatt/Archiv/2009/April",
"wiki": "de.wikipedia.org",
"url": "https://de.wikipedia.org/wiki/Wikipedia:Fotowerkstatt/Archiv/2009/April"
}
]
},
// ...snip...
Note that you will very likely have to deal with query continuations, since there may easily be more results than MediaWiki will return in a single request. See the linked page for more information on handling those (or just use an MW API client that handles them for you).
I don't understand your use case ("our collections"?) so I don't know why you want to use the API directly, but if you want to recurse in categories you're going to do a lot of wheel reinvention.
Most people use the tools made by Magnus Manske, creator of MediaWiki: in this case it's GLAMourous. Example with 3 levels of recursion (finds 186k images, 114k usages): https://tools.wmflabs.org/glamtools/glamorous.php?doit=1&category=Automobiles&use_globalusage=1&depth=3
Results can also be downloaded in XML format, so it's machine-readable.
We have a scenario where we need to store multiple feeds under a site model as following:
{
id: site_id
name: site_name
feeds: [
{
url: feed_url_1
date: feed_update_date_1
},
{
url: feed_url_2
date: feed_update_date_2
},
...
]
}
Since feeds is an array, we can update it with $set, $push or $addToSet.
2 different race condition (write skew) may occur when our concurrent application (queue) try to update the same site model.
If we pick $set, and guard duplicate on client side, then if 2 queues are writing to the same site, one feed maybe lost with following sequence.
Given a wordpress site, extract 2 feeds (RSS and ATOM), dispatch to Q1 and Q2.
Q1: load existing feed, check RSS feed is new
Q2: load existing feed, check ATOM feed is new
Q1: $set feeds => [RSS]
Q2: $set feeds => [ATOM]
Now RSS feed is lost.
If we pick $push or $addToSet, then following may happen.
User A added a site, putting RSS feed to Q1
User B added the same site, putting the same RSS feed to Q2
Q1: load existing feed, check RSS feed is new
Q2: load existing feed, check RSS feed is new
Q1: $push RSS
Q2: $push RSS
Now RSS feed has been duplicated
If our data model were simply { url }, then $addToSet will safeguard against duplicate feed. But unfortunately this is not the case, the date attribute may differ. So $addToSet is not much safer than $push.
We have thought of a few possible workaround to this problem, but none are great given our tight schedule.
Decouple feeds from site into its own collection, safeguard with url alone, and change our model and repository accordingly.
Insert a partial { url } into the site model first, then update them with addition information, this should makes $addToSet usable, but may break other queue that require date to always be present (testing needed).
Let race condition happen as-is, $push the feed first, use a background queue to detect duplicate and remove them later.
(There might be a 4th solution if upsert work with positional query, but as far as I know MongoDB v2.4 doesn't have it yet)
So I wonder whether there are better alternative for resolving this kind of race condition. Or if there are some best practices for it.
you might want to have a look at tokumx, a fork of mongodb which supports transactions (besides a few other usefull things)
You can use a gard on the update selector:
alice(mongod-2.4.8) test> db.foo.save({_id: 12 })
Updated 1 new record(s) in 1ms
alice(mongod-2.4.8) test> db.foo.update({ _id: 12, "feeds.url" : {$ne: "baz"} },
{ $push : { feeds : { url: "baz" } } } )
Updated 1 existing record(s) in 1ms
alice(mongod-2.4.8) test> db.foo.update({ _id: 12, "feeds.url" : {$ne: "baz"} },
{ $push : { feeds : { url: "baz" } } } )
Updated 0 record(s) in 1ms
alice(mongod-2.4.8) test> db.foo.find({_id: 12 })
{
"_id": 12,
"feeds": [
{
"url": "baz"
}
]
}
Fetched 1 record(s) in 1ms -- Index[_id_]
I have a table in my database called timeline_entries. This table contains the following fields:
id, headline, text, startDate, type, media, caption, credit. The id field is used for referencing individual entries through a CMS.
I have worked out how to export the data as JSON and save to a file, but I'm struggling to find a way to format it into the following structure;
{
"timeline":
{
"headline":"value",
"type":"default",
"startDate":"value",
"text":"value",
"asset":
{
"media":"value",
"credit":"value",
"caption":"value"
},
"date": [
{
"startDate":"value",
"type":"",
"headline":"value",
"text":"value",
"asset":
{
"media":"value",
"credit":"value",
"caption":"value"
}
},
{
"startDate":"value",
"type":"",
"headline":"value",
"text":"value",
"asset":
{
"media":"value",
"credit":"value",
"caption":"value"
}
},
{
"startDate":"value",
"type":"",
"headline":"value",
"text":"value",
"asset":
{
"media":"value",
"credit":"value",
"caption":"value"
}
},
]
}
}
(Please ignore the shoddy indenting, I'm still getting used to this!)
I've had to replace the actual data with 'value' as some of the data is quite long.
As you can see, the first set of data needs to be formatted slightly different to the rest, the remainder of the sets placed within "date" and then the media, caption and credit fields need to be structured as a subset of "asset".
There will be more rows of data than just four or so, so I can't hardcode anything.
Can anyone help me format it? If it's possible, I'd like to keep the database side as simple as possible, but it can be changed if I have to. Perhaps I'm going about this completely wrong? Any help would be much appreciated.
Thank you.
Formatting of JSON isn't at all important other than for aesthetics, have you thought of just using the php json_encode method and have done with it?
Would there be a way to do a zip code lookup based on City/State input in a form? I'm thinking the Google geocode API might be the right direction. Any thoughts? I have a site built on Wordpress so the code would have to utilize PHP. Thanks in advance.
YQL can do things like this:
select name from geo.places.children where parent_woeid in (select woeid from geo.places where text="sunnyvale, usa" limit 1) AND placetype = 11
returns:
{
"query": {
"count": 6,
"created": "2011-03-16T06:49:09Z",
"lang": "en-US",
"results": {
"place": [
{
"name": "94086"
},
{
"name": "94087"
},
{
"name": "94088"
},
{
"name": "94089"
},
{
"name": "94090"
},
{
"name": "94085"
}
]
}
}
}
YQL Console
There are examples on there on how to implement queries like this in both PHP and Javascript on their site.
Geocoding is where you find the coordinates of an address. Yes you could geocode a city,state but this would give you he center of the city (as defined by the geocoder's internal database - typically a centroid or 'city hall'.
Most cities have multiple zip codes: Do you want all of these?
Similarly a zip code could contain multiple cities - especially in rural areas where zip codes can be large and cities are what other countries would call 'villages' and 'hamlets'
So you best bet is probably to get a database. There might be some free ones around (Geonames comes to mind but I don't think it has zip codes), but you might end up having to buy one.
First a note on the Google API: be aware of Google's TOS so you don't take a wasted path as others have done (sometimes unknowingly). Specifically: "Note: the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited.".
Your best bet is to get a free zip code database if your project is not mission-critical; otherwise, you'll probably need a good commercial-grade database. Just google "commercial grade zip code database".
Also, see a good stack-overflow thread about this topic.