Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2016-11-03 17:27:36

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

Howdy, folks!

I have a site that uses a custom field to associate multiple authors with an article. In data modeling terms, the authors and articles have a many-to-many relationship. But I’m using Textpattern, the tables aren’t perfectly normalized: Author information is in the txp_users table (along with smd_bio) and article info is in the textpattern table.

Prior to Textpattern 4.6, one of my article templates fetched the list of author names from the txp_user table as follows:

php 
<txp:if_custom_field name="authors">

  <txp:php>
  //<?php

    global $thisarticle;

    $pax_author_list = $thisarticle['authors'];

    $pax_author_array = explode(',', $pax_author_list);

    while (list($key, $val) = each($pax_author_array)) {

      $pax_author_ref = trim($val);

      $q = 'SELECT `RealName` FROM `txp_users` WHERE `name`="'.$pax_author_ref.'"';

      $rs = safe_query($q) or die(mysql_error());

      while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
        $pax_author_realname = $row['RealName'];
        echo 'Author-Name: '.$pax_author_realname.n;
      }

    }

  //?>
  </txp:php>

</txp:if_custom_field>

When I run this under PHP 5.6.27, it fails silently. But PHP 7.0.12 tells me that the mysql_fetch function is undefined.

My hunch is that this function was nixed in the query overhaul in Textpattern 4.6.0, but is there a similar function available to the txp:php tag in the latest version?

What is the simplest way to resolve this?

And for the longer term, is there a better way to model many-to-many relationships between articles and authors in future? (Revising my data model wouldn’t be something I could take on right away, but maybe some day.)

Thank you!

Edit: Fix Textile

Last edited by johnstephens (2016-11-03 17:28:05)

Offline

#2 2016-11-03 17:36:06

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Re: mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

Hi John, Admittedly I don’t fully understand you code but couldn’t this be achieved with native tags?


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2016-11-03 17:55:50

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

Thanks, Yiannis! I didn’t explain the situation very well.

As far as I know Textpattern has never natively supported assigning an article to more than one author. That’s why I’m using the custom field along with smd_bio.

Suppose you and I work on an article together. In the “authors” field, we would list our Textpattern logins, as follows: “colak, johnstephens”.

If I output the custom field as-is, it would give me the same: “colak, johnstephens”.

To get anything like a natural byline, or a list of author bios at the end, we need to get the associate information from the txp_user table or smd_bio. For the complicated instances, I use smd_each to iterate over the author names, then the smd_bio tags.

But for the template above, all I needed was the RealName associated with each author, and before Txp 4.6 it was easier to get that with simple PHP.

Does that make better sense now? Are there obvious native tags for this that I’m overlooking?

Thank you again.

Offline

#4 2016-11-03 18:27:01

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

Maybe <txp:authors name='<txp:custom_field name="authors" />' /> will do?

Edit: more exactly

<txp:authors name='<txp:custom_field name="authors" />'>
    Author-Name: <txp:author />
</txp:authors>

Last edited by etc (2016-11-03 18:30:57)

Offline

#5 2016-11-03 19:00:35

kuopassa
Plugin Author
From: Porvoo, Finland
Registered: 2008-12-03
Posts: 228
Website

Re: mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

Maybe the problem is using mysql instead of mysqli. Bloke wrote something about this a month ago over here.

Offline

#6 2016-11-03 19:33:55

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

Thank you both! Looking at the docs for txp:authors, it looks like the perfect tag for the job! But there doesn’t seem to be a way to sort the authors in the order they are listed in the name attribute. Am I missing something?

If I can output the txp:authors in the same order as the name attribute, I that would be a perfect replacement for pretty much all my smd_each needs on this site!

Changing mysql to mysqli seems to work as a revision to my original code.

Offline

#7 2016-11-03 19:37:27

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

As an example of a tag that lets you control the sort order the way I need, the sections attribute for txp:section_list establishes an order for the output.

Offline

#8 2016-11-03 21:10:45

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

I’ve got this little plugin that could help:

<txp:authors name='<txp:custom_field name="authors" />'
    sort='<txp:etc_sort field="name" order=''<txp:custom_field name="authors" />'' />'
>
    Author-Name: <txp:author />
</txp:authors>
# Name: etc_sort v0.2 
# Type: Public plugin
# 
# Author: 
# URL: 
# Recommended load order: 5

# .....................................................................
# This is a plugin for Textpattern CMS - http://textpattern.com/
# To install: textpattern > admin > plugins
# Paste the following text into the 'Install plugin' box:
# .....................................................................

YToxMTp7czo0OiJuYW1lIjtzOjg6ImV0Y19zb3J0IjtzOjY6ImF1dGhvciI7czowOiIiO3M6
MTA6ImF1dGhvcl91cmkiO3M6MDoiIjtzOjc6InZlcnNpb24iO3M6MzoiMC4yIjtzOjExOiJk
ZXNjcmlwdGlvbiI7czowOiIiO3M6NDoiY29kZSI7czo2MjA6Ii8vIFRYUCA0LjYgdGFnIHJl
Z2lzdHJhdGlvbg0KaWYgKGNsYXNzX2V4aXN0cygnXFRleHRwYXR0ZXJuXFRhZ1xSZWdpc3Ry
eScpKSB7DQoJVHhwOjpnZXQoJ1xUZXh0cGF0dGVyblxUYWdcUmVnaXN0cnknKS0+cmVnaXN0
ZXIoJ2V0Y19zb3J0Jyk7DQp9DQoNCmZ1bmN0aW9uIGV0Y19zb3J0KCRhdHRzKSB7DQoJZXh0
cmFjdChsQXR0cyhhcnJheSgNCgkJJ2ZpZWxkJz0+J1Bvc3RlZCcsDQoJCSduYXRpdmUnPT4n
MCcsDQoJCSdvcmRlcic9Pm51bGwNCgkpLCRhdHRzKSk7DQoNCglpZihlbXB0eSgkb3JkZXIp
KSByZXR1cm4gJGZpZ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DQzNjllMjFhYzBlZWE3OWJiODRjMTM1ODBhZTIiO30=

Offline

#9 2016-11-04 08:20:13

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,076
Website

Re: mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

Oleg, you little plugin is just the missing piece I needed. Last week, I had gotten as far as doing what you suggested above with a custom field and <@txp:authors />, but the sorting remained problematic. Now I just need to have the authors remember to fill in the main author first, then the secondary author(s) (aka guest authors).


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

#10 2016-11-04 09:00:49

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

Hi Philippe, glad it helps. Actually, the plugin (v.0.1) is there since few years, but I’ve forgotten about it.

Offline

#11 2016-11-04 17:23:09

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

That works brilliantly, etc—thank you! Based on the post kuopassa highlighted, I’m guessing that this will be more robust than merely changing my PHP code to use mysqli_* functions, right?

Thanks again!

Offline

#12 2016-11-04 17:31:04

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: mysql_fetch_array missing in Txp 4.6.x—How can I fix my template?

Oh, and I realized the txp:authors tag can’t replace all my uses of smd_each, because I need access to the index of the array in some cases.

In bylines, my template needs to output the conjunction based on how many authors there are, like so:

1 author: by John
2 authors: by John and Yiannis
3 or more authors: by John, Yiannis, and Etc

My current template for bylines works like this:

<txp:smd_each
	type="field"
	include="authors"
	subset="2">

	<txp:php>
		if ({smd_var_total} > 1 && {smd_var_counter} === {smd_var_total}) echo ' and ';
	</txp:php>

	<txp:smd_bio_author
		author="{smd_var_value}"
		form="author_name"
		labeltag=""
		wraptag=""/>

	<txp:php>
		if ({smd_var_total} > {smd_var_counter} && {smd_var_total} !== 2) echo ', ';
	</txp:php>

</txp:smd_each>

Since smd_each has counters for the current item in the array and the whole array, I can just compare those to get the right conjunction.

If there’s a way to do that with the new txp:authors tag, cool! But that seems like too much work for the authors tag to do.

Last edited by johnstephens (2016-11-04 17:32:13)

Offline

Board footer

Powered by FluxBB